首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >统一摄像机运动

统一摄像机运动
EN

Stack Overflow用户
提问于 2020-01-11 12:48:15
回答 1查看 1.4K关注 0票数 0

我正在复习这段关于摄像机运动的代码:

代码语言:javascript
复制
 using UnityEngine;
 using System.Collections;

 public class CamMove : MonoBehaviour {
    Vector2 mouseLook;
    Vector2 smoothV;
    public float sensitivity=5.0f;
    public float smoothing = 2.0f;

    GameObject character;
    void Start()
    {
        //moving left and right
        character = this.transform.parent.gameObject;
    }

    void Update()
    {
        var md = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y")); //mouse movement
        md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));
        smoothV.x = Mathf.Lerp (smoothV.x, md.x, 1f / smoothing);



        smoothV.y = Mathf.Lerp (smoothV.y, md.y, 1f / smoothing);

        mouseLook += smoothV;
        if(mouseLook.y>-40 && mouseLook.y<60)
                    transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, Vector3.right);

        character.GetComponent<CharacterController>().transform.localRotation = Quaternion.AngleAxis (mouseLook.x, character.transform.up);
    }


 }

他怎么能得到每一个新的地方?用Math.Lerp插值?另外,我也不明白md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));这个部分,也不明白这个部分:

代码语言:javascript
复制
if(mouseLook.y>-40 && mouseLook.y<60)
                    transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, Vector3.right);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-11 14:18:20

代码语言:javascript
复制
var md = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y")); //mouse movement

甚至是评论mouse movement。像往常一样,一个更好的变量名已经可以解释它了。更好的名字是mouseDelta。因此,代码没有使用固定的鼠标位置,而是使用自上一帧以来移动的实例。(见Input.GetRawAxis)

然后

代码语言:javascript
复制
md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));

Vector2.Scale正在向上或向下扩展这个向量。你也可以把它写成

代码语言:javascript
复制
md = new Vector2 (md.x * sensitivity * smoothing, md.y * sensitivity * smoothing);

实际上,这是非常不必要的,因为您可以这样简单地编写它:

代码语言:javascript
复制
md *= sensitivity * smoothing;

然后Mathf.Lerp是两个给定位置之间的线性插值,在01之间使用一定的factor,其中0将是完全的第一个参数,1完全是第二个参数,或者是介于两者之间的任何参数。例如,一个因子0.5将导致两个值之间的中心,因此这取决于您给定的smoothing

代码语言:javascript
复制
    smoothV.x = Mathf.Lerp (smoothV.x, md.x, 1f / smoothing);
    smoothV.y = Mathf.Lerp (smoothV.y, md.y, 1f / smoothing);

同样,这是不必要的,因为它更好地直接使用Vector2.Lerp编写。

代码语言:javascript
复制
 smoothV = Vector2.Lerp(smoothV, md, 1f/smoothing);

但实际上,我不太确定这样做的效果,因为这不是绝对值,但你后来在上添加了的每一个帧,所以在它上使用Lerp对我来说没有多大意义。我可能会直接使用Lerp将当前的mouseLoock移动到新的目标值。

最后你做了

代码语言:javascript
复制
mouseLook += smoothV;
if(mouseLook.y>-40 && mouseLook.y<60)
    transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, Vector3.right);

character.GetComponent<CharacterController>().transform.localRotation = Quaternion.AngleAxis (mouseLook.x, character.transform.up);

更新两个轮转。在您的代码中没有指定任何新的职位.

除了GetComponent在帧上的使用效率极低外,您还应该将该引用存储一次(例如,在Start中),然后再重用它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59694526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档