c# - 3d 鼠标瞄准相机第三人称垂直 C#

标签 c# unity-game-engine 3d camera

我正在尝试制作鼠标瞄准相机,它可以水平和垂直旋转我的玩家,看着玩家并保持恒定的距离。 在那个版本中它工作正常,但我也无法使其水平。 每个版本的相机都会卡住,我会自行旋转播放器。 我是编程新手,因此将适当的 target.transform.eulerAngles.y 也分配给垂直方向可能是一项简单的任务,但我做不到。

public class MouseAimCamera : MonoBehaviour {
    public GameObject target;
    public float rotateSpeed = 5;
    Vector3 offset;

    void Start() {
        offset = target.transform.position - transform.position;
    }

    void LateUpdate() {
        float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
        target.transform.Rotate(0, horizontal, 0);

        float desiredAngle = target.transform.eulerAngles.y;
        Quaternion rotation = Quaternion.Euler(0, desiredAngle, 0);
        transform.position = target.transform.position - (rotation * offset);

        transform.LookAt(target.transform);
    }
}

如果你们能帮助我,我会很高兴。

最佳答案

这将在世界空间中旋转,这在某些角度可能会感觉不对,因为相机并不总是笔直向上。

public class MouseAimCamera : MonoBehaviour {
    public GameObject target;
    public float rotateSpeed = 5;

    void Start() {
        transform.parent = target.transform;
        transform.LookAt(target.transform);
    }

    void LateUpdate() {
        float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
        float vertical = Input.GetAxis("Mouse Y") * rotateSpeed;
        target.transform.RotateAround(target.transform.position, Vector3.up, horizontal);
        target.transform.RotateAround(target.transform.position, Vector3.left, vertical);
    }
}

这将在本地空间中旋转,并且可能感觉更自然,具体取决于您要构建的内容。这与您原来的解决方案很接近,所以我猜这不是您想要的。

public class MouseAimCamera : MonoBehaviour {
    public GameObject target;
    public float rotateSpeed = 5;

    void Start() {
        transform.parent = target.transform;
        transform.LookAt(target.transform);
    }

    void LateUpdate() {
        float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
        float vertical = Input.GetAxis("Mouse Y") * rotateSpeed;
        target.transform.Rotate(vertical, horizontal, 0);
    }
}

关于c# - 3d 鼠标瞄准相机第三人称垂直 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30722170/

相关文章:

python - 计算 3D(或 n-D)质心的最佳方法是什么?

javascript - 如何在 HTML5 Canvas 中显示 tQuery.World

android - 获取android : Unity3d的音量大小

C# 使用实现接口(interface)的类而不添加对定义接口(interface)的程序集的引用

c# - SentrySDK VB.NET初始化示例

c# - 如何为相同类型的 UIElement 编写通用事件处理程序?

c# - 无法在 Unity3D 中移动实例化的预置

unity-game-engine - 使用 Unity 3D 移动鼠标时相机跟随玩家

css - 使用 PhantomJS 的 CSS3 的 3D 问题

c# - 删除和创建新的/覆盖同名的文件 c# .NET