简体   繁体   English

Unity 3d 运动不适用于相机方向

[英]Unity 3d movement not working with Camera direction

I have two scripts one is the MouseHandler and the other is the SimpleMovement.我有两个脚本,一个是 MouseHandler,另一个是 SimpleMovement。 rotating the camera works and moving works however when the camera turns the movement doesn't go in that direction.旋转相机有效,移动有效,但是当相机转动时,该方向的运动不会 go。 EG i turn the camera 90 degrees to the right but the forward doesn't change.例如,我将相机向右旋转 90 度,但前进方向不变。 The forward doesn't go to where the camera is facing.前方没有 go 到相机所面对的位置。 Sorry if i'm just being stupid.对不起,如果我只是愚蠢。 Any help would be appreciated任何帮助,将不胜感激

MouseHandler script: MouseHandler 脚本:

public class MouseHandler : MonoBehaviour
{
    // horizontal rotation speed
    public float horizontalSpeed = 1f;
    // vertical rotation speed
    public float verticalSpeed = 1f;
    private float xRotation = 0.0f;
    private float yRotation = 0.0f;
    private Camera cam;

    void Start()
    {
        cam = Camera.main;
    }

    void Update()
    {
        float mouseX = Input.GetAxis("Mouse X") * horizontalSpeed;
        float mouseY = Input.GetAxis("Mouse Y") * verticalSpeed;

        yRotation += mouseX;
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90, 90);

        cam.transform.eulerAngles = new Vector3(xRotation, yRotation, 0.0f);
    }
}

SimpleMovement Script:简单运动脚本:

public class SimpleMovement : MonoBehaviour
{
    CharacterController characterController;
    public float MovementSpeed = 1;
    public float Gravity = 9.8f;
    private float velocity = 0;

    private void Start()
    {
        characterController = GetComponent<CharacterController>();
    }

    void Update()
    {
        // player movement - forward, backward, left, right
        float horizontal = Input.GetAxis("Horizontal") * MovementSpeed;
        float vertical = Input.GetAxis("Vertical") * MovementSpeed;
        characterController.Move((Vector3.right * horizontal + Vector3.forward * vertical) * Time.deltaTime);

        // Gravity
        if (characterController.isGrounded)
        {
            velocity = 0;
        }
        else
        {
            velocity -= Gravity * Time.deltaTime;
            characterController.Move(new Vector3(0, velocity, 0));
        }
    }
}

First, get a reference to the main cmaera and cache it, because you're going to be referencing it frequently, and simply using Camera.main is a bit expensive:首先,获取对主 cmaera 的引用并将其缓存,因为您将经常引用它,并且简单地使用Camera.main有点昂贵:

private Camera mainCam;

...

private void Start()
{
    characterController = GetComponent<CharacterController>();
    mainCam = Camera.main;
}

Then, use mainCam.transform.right and mainCam.transform.forward but with the y set to 0 and normalized instead of Vector3.right and Vector3.forward .然后,使用mainCam.transform.rightmainCam.transform.forward但将y设置为 0 并进行归一化,而不是Vector3.rightVector3.forward This will make the movement be based on the rotation of the camera:这将使运动基于相机的旋转:

float horizontal = Input.GetAxis("Horizontal") * MovementSpeed;
float vertical = Input.GetAxis("Vertical") * MovementSpeed;
Vector3 camRightFlat = new Vector3(mainCam.transform.right.x, 0f, 
        mainCam.transform.right.z).normalized;
Vector3 camForwardFlat = new Vector3(mainCam.transform.forward.x, 0f,
        mainCam.transform.forward.z).normalized;
characterController.Move(
        (camRightFlat * horizontal + camForwardFlat  * vertical) * Time.deltaTime);

Altogether:共:

public class SimpleMovement : MonoBehaviour
{
    CharacterController characterController;
    public float MovementSpeed = 1;
    public float Gravity = 9.8f;
    private float velocity = 0;
    private Camera mainCam;

    private void Start()
    {
        characterController = GetComponent<CharacterController>();
        mainCam = Camera.main;
    }

    void Update()
    {
        // player movement - forward, backward, left, right
        float horizontal = Input.GetAxis("Horizontal") * MovementSpeed;
        float vertical = Input.GetAxis("Vertical") * MovementSpeed;
        Vector3 camRightFlat = new Vector3(mainCam.transform.right.x, 0f, 
                mainCam.transform.right.z).normalized;
        Vector3 camForwardFlat = new Vector3(mainCam.transform.forward.x, 0f, 
                mainCam.transform.forward.z).normalized;
        characterController.Move((camRightFlat * horizontal + camForwardFlat  * vertical) 
                * Time.deltaTime);

        // Gravity
        if (characterController.isGrounded)
        {
            velocity = 0;
        }
        else
        {
            velocity -= Gravity * Time.deltaTime;
            characterController.Move(new Vector3(0, velocity, 0));
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM