【问题标题】:unity - camera movementunity - 相机运动
【发布时间】:2020-01-11 12:48:15
【问题描述】:

我正在查看这段关于相机移动的代码:

 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));的部分 还有部分:

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

【问题讨论】:

  • 如有疑问,请按照代码进行操作。如果您不明白它的作用,请查看统一调用,打印出值...并观察会发生什么
  • 代码所做的只是在使用鼠标位置调用时左右移动相机。它只关注 Y 范围 -40 到 60。

标签: c# unity3d camera


【解决方案1】:

var md = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y")); //mouse movement

甚至评论mouse movement。像往常一样,一个更好的变量名称已经可以解释它。最好叫mouseDelta。因此代码不使用固定的鼠标位置,而是使用自上一帧以来的行进距离。 (见Input.GetRawAxis


然后

md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));

Vector2.Scale 正在放大或缩小此向量。你也可以写成

md = new Vector2 (md.x * sensitivity * smoothing, md.y * sensitivity * smoothing);

其实这是完全没有必要的,因为你可以这样写更简单:

md *= sensitivity * smoothing;

那么Mathf.Lerp是两个给定位置之间的线性插值,使用01之间的某个factor,其中0完全是第一个参数,1完全是第二个参数,否则介于两者之间的任何东西。例如。一个因素0.5 将导致两个值之间的中心,所以这取决于你给定的smoothing

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

同样,这是非常不必要的,因为最好直接使用Vector2.Lerp 编写

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

但实际上我不太确定这是否能达到您的预期,因为这不是绝对值,但您稍后会在每一帧 添加,因此在其上使用 Lerp 并没有多大意义无论如何对我来说都是有意义的......我可能会直接使用Lerp 将当前的mouseLoock 移动到新的目标值。


你终于做到了

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);

更新两个轮换。您的代码中根本没有分配任何新的 position ...


除了 GetComponent 的逐帧使用效率极低之外,您应该将该引用 一次 存储(例如在 Start 中)并在以后重复使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-07
    • 2015-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多