【问题标题】:Unity3d Camera rotation not workingUnity3d相机旋转不起作用
【发布时间】:2023-03-04 16:56:01
【问题描述】:

每当我按下 Q 或 E 时,我试图让粘在播放器上的相机将其旋转 45 度。但由于某种原因,我无法让它工作。我正在使用 C#。

using UnityEngine;
using System.Collections;
public class Camera : MonoBehaviour 
{
    int rotatespeed = 3;
    int rotationstart = 90;

    public GameObject player;

    private Vector3 offset;

    // Use this for initialization
    void Start()
    {
        offset = transform.position - player.transform.position;
    }     

    // Update is called once per frame
    void LateUpdate() {
            transform.position = player.transform.position + offset;
    }

    void Update()
    {
        if (Input.GetKey("q"))
        {
            Camera.main.transform.rotation = Quaternion.Euler(x + 45 , y, z);
        }
        if (Input.GetKey("e"))
        {
            Camera.main.transform.rotation = Quaternion.Euler(x - 45, y, z);
        }
    }
}

【问题讨论】:

    标签: c# unity3d camera rotation


    【解决方案1】:

    旋转时应该乘法

        if (Input.GetKey("q"))
        {
            Camera.main.transform.rotation *= Quaternion.Euler(45 , 0, 0);
        }
        if (Input.GetKey("e"))
        {
            Camera.main.transform.rotation *= Quaternion.Euler(-45, 0, 0);
        }
    

    但是这使它看起来上下,如果你想左右使用以下

        if (Input.GetKey("q"))
        {
            Camera.main.transform.rotation *= Quaternion.Euler(0, 45, 0);
        }
        if (Input.GetKey("e"))
        {
            Camera.main.transform.rotation *= Quaternion.Euler(0, -45, 0);
        }
    

    重要的旁注

    请记住,GetKey 将在用户按住它时返回 true,即使是非常快速的按下也会导致您的相机似乎失去控制,因为它仍然是多个帧。您很可能希望使用GetKeyDown,它只会在用户每次按键时返回 true 一次

    【讨论】:

    • 好吧,我改变了一点,我也将它设置为 javascript 而不是 C#。但现在相机正在旋转,而不是围绕角色旋转。你知道如何让它绕着一个固定点旋转吗?
    猜你喜欢
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多