【问题标题】:Why is my FPS character turning to 0 on the Y axis when I have it set to 90为什么当我设置为 90 时,我的 FPS 角色在 Y 轴上变为 0
【发布时间】:2019-07-24 10:10:50
【问题描述】:

我正在使用 FPS 控制器,由于某种原因,当我点击播放时,当我将其旋转到 90 时,播放器在 y 轴上一直旋转到 0。我很确定它与相机有关,如果这有帮助的话缩小范围。

代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
Animator anim;
float RotateX;
float RotateY;
public static bool GamePaused;

[SerializeField]
[Header("Game Objects")]
public GameObject Camera;

[Header("Movement Settings")]
public float WalkSpeed = 5.0f;


[Header("Rotation Settings")]
public float RorationSpeed = 3.0f;
public float MaxYAxis = 60.0f;
public float MinYAxis = -48.0f;

private void Start()
{
    anim = GetComponent<Animator>();
}

void Update()
{
    Rotation();
    Movement();
}

void Rotation()
{
    RotateX += Input.GetAxis("Mouse X") * RorationSpeed;
    RotateY += Input.GetAxis("Mouse Y") * RorationSpeed;
    RotateY = Mathf.Clamp(RotateY, MinYAxis, MaxYAxis);
    Camera.transform.localRotation = Quaternion.Euler(-RotateY, 0f, 0f);
    transform.rotation = Quaternion.Euler(0f, RotateX, 0f);
}

void Movement()
{
    transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * WalkSpeed * Time.deltaTime);
    transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * WalkSpeed * Time.deltaTime);

}
}

【问题讨论】:

    标签: c# unity3d camera rotation


    【解决方案1】:

    Update 的第一次调用中,GetAxis("Mouse X") 返回0 所以

    transform.rotation = Quaternion.Euler(0f, RotateX, 0f);
    

    将旋转重置为0, 0, 0


    您应该简单地存储初始旋转

    private void Awake()
    {
        RotateX = transform.rotation.eulerAngles.y;
        RotateY = Camera.transform.localRotation.eulerAngles.x;
        if (RotateY > 180)
        {
            RotateY = -(360 - RotateY);
        }
    }
    
    private void Rotation()
    {
        RotateX += Input.GetAxis("Mouse X") * RorationSpeed;
        RotateY -= Input.GetAxis("Mouse Y") * RorationSpeed;
        RotateY = Mathf.Clamp(RotateY, MinYAxis, MaxYAxis);
        Camera.transform.localRotation = Quaternion.Euler(RotateY, 0f, 0f);
        transform.rotation = Quaternion.Euler(0f, RotateX, 0f);
    }
    

    【讨论】:

    • 谢谢你,这真的很好用。我在您的回复中将相机变换改回 -RotateY 否则当您抬头时鼠标会下降。
    • 使用RotateY -= ...应该已经是这种情况了,注意-
    • 我完全错过了。
    【解决方案2】:

    这实际上与您的Rotation 方法有关。 游戏一开始会调用Update方法,会调用Rotation方法。在那里你更新旋转的值,在游戏开始时它将是 0.0 并且输入轴上没有移动,所以一开始没有旋转,最后你用 0.0 值更新你的旋转,重置你在编辑器。

    您想要做的是将相机或控制器旋转RotateYRotateX 度数。比如:

    void Rotation()
    {
        RotateX = Input.GetAxis("Mouse X") * RorationSpeed;
        RotateY = Input.GetAxis("Mouse Y") * RorationSpeed;
        RotateY = Mathf.Clamp(RotateY, MinYAxis, MaxYAxis);
        Camera.transform.Rotate(new Vecto3(-RotateY, 0f, 0f));
        transform.Rotate(new Vector3(0f, RotateX, 0f));
    }
    
    

    【讨论】:

    • @Sealeas 它只是让我以越来越快的速度转圈。
    • 哦!我的错误,我在每次更新时都增加了轮换。我更新了答案。通过这种方式,您甚至可以只在 Rotation 方法中使用局部变量而不是类属性,因为您不需要存储 te 值。
    • 它可以工作,但现在相机的限制不起作用。
    • 没错。现在您需要从变换中获取当前旋转并钳制该值,RotationY 只是帧之间发生的旋转,而不是对象的当前旋转。
    猜你喜欢
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多