【问题标题】:Translate camera to rotate left ('Q') and right ('E')平移相机以向左('Q')和向右('E')旋转
【发布时间】: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 中,用于跟随玩家,现在我想调整代码以便它同时执行,跟随玩家并在按钮点击时平移相机,当我在这种情况下按下键时什么都没有发生了,我感觉是因为后期更新变换,但我不知道如何解决这个问题。

【问题讨论】:

  • 正如您代码中的注释中所述,LateUpdateUpdate 之后调用,并且正在重置您的相机位置。
  • 是的,我知道这是个问题,我该如何解决,所以我可以做两件事
  • Update 函数中简单地更改offset 而不是transform.position 怎么样?
  • 您可以将所有代码移动到这些方法之一。例如,在我的 Unity 程序中,所有相机控制内容都发生在 LateUpdate 中,并且没有 Update 方法。
  • 按照你们的建议做了,但仍然没有与相机翻译相关的任何事情发生

标签: c# unity3d camera


【解决方案1】:

如果您希望相机围绕播放器运行(如您的评论中所述),请在您的 cameraMove 的脚本 Update() 方法中:

if (Input.GetKey(KeyCode.Q))
{
  transform.RotateAround(player.transform.position, player.transform.up, moveSpeed * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.E))
{
  transform.RotateAround(player.transform.position, player.transform.up, -moveSpeed * Time.deltaTime);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    相关资源
    最近更新 更多