【发布时间】:2018-08-22 01:23:08
【问题描述】:
当我按下 Q 和 E 时,我正在尝试平移相机,例如,当我按下“q”时,相机会向左平移并让我给出一个类似于相机旋转的动作,以执行 我试过这个:
public class cameraMove : MonoBehaviour {
public GameObject player; //Public variable to store a reference to the player game object
private float moveSpeed;
private Vector3 offset; //Private variable to store the offset distance between the player and camera
// Use this for initialization
void Start ()
{
//Calculate and store the offset value by getting the distance between the player's position and camera's position.
offset = transform.position - player.transform.position;
}
void Update(){
if (Input.GetKey (KeyCode.Q))
transform.position += Vector3.up * moveSpeed * Time.deltaTime;
else if (Input.GetKey (KeyCode.E))
transform.position += -Vector3.up * moveSpeed * Time.deltaTime;
}
// LateUpdate is called after Update each frame
void LateUpdate ()
{
// Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
transform.position = player.transform.position + offset;
}
}
我已经有代码存在于 LateUpdate 中,用于跟随玩家,现在我想调整代码以便它同时执行,跟随玩家并在按钮点击时平移相机,当我在这种情况下按下键时什么都没有发生了,我感觉是因为后期更新变换,但我不知道如何解决这个问题。
【问题讨论】:
-
正如您代码中的注释中所述,
LateUpdate在Update之后调用,并且正在重置您的相机位置。 -
是的,我知道这是个问题,我该如何解决,所以我可以做两件事
-
在
Update函数中简单地更改offset而不是transform.position怎么样? -
您可以将所有代码移动到这些方法之一。例如,在我的 Unity 程序中,所有相机控制内容都发生在
LateUpdate中,并且没有Update方法。 -
按照你们的建议做了,但仍然没有与相机翻译相关的任何事情发生