【发布时间】: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。