我正在复习这段关于摄像机运动的代码:
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);发布于 2020-01-11 14:18:20
井
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是两个给定位置之间的线性插值,在0和1之间使用一定的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);更新两个轮转。在您的代码中没有指定任何新的职位.
除了GetComponent在帧上的使用效率极低外,您还应该将该引用存储一次(例如,在Start中),然后再重用它。
https://stackoverflow.com/questions/59694526
复制相似问题