统一-相机运动

时间:2020-01-11 12:48:15

标签: c# unity3d camera

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

$url = 'http://sms.domain.com/Api.aspx?usr=abcapi&pwd=pass123&smstype=TextSMS&to='.$phonesms.'&rout=Transactional&from=SMSACT&msg='.urlencode($msg);

他如何获得新的职位?使用Math.Lerp插值吗?我也无法理解 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); } } 部分 还有一部分:

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

1 个答案:

答案 0 :(得分: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正在缩放此Vector。您也可以将其写为

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

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

md *= sensitivity * smoothing;

然后Mathf.Lerp是两个给定位置之间的线性插值,使用factor0之间的某个1,其中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中)存储该引用,并在以后重用。