简体   繁体   中英

Unity 3D instantly rotate in the direction of movement

I have a somewhat similar question to this one , I'm trying to rotate in the direction of movement but the issue is that I want to maintain my position and just instantly rotate because I have a procedural maze and there's not enough space to gradually swing around in the direction of movement in a wide turn. I'll post code attempts at the bottom, attempt two gives the error: viewing vector is zero.

If you look at all four of these screenshots, when the y-axis is rotated, the x-axis and z-axis jump to a new position. When I'm moving this ends up looking like I'm teleporting around the maze. Anyone know how to rotate while remaining in the same position?

Here's right: 在此处输入图像描述

Here's forward: 在此处输入图像描述

Here's left: 在此处输入图像描述

Here's back: 在此处输入图像描述

here's code attempt 1:

public void playerMovement()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        Vector3 movementx = new Vector3(horizontalInput, 0f, 0f);
        Vector3 movementy = new Vector3(0f, 0f, verticalInput);
      
        movementy = transform.forward * verticalInput;
        movementx = transform.right * horizontalInput;

        if (horizontalInput > 0)
        {
            transform.eulerAngles = new Vector3(0f, 90f, 0f);
            transform.Translate(-movementx.normalized * 0.1f, Space.Self);
        }

        if (horizontalInput < 0)
        {
            transform.eulerAngles = new Vector3(0f, 270f, 0f);
            transform.Translate(-movementx.normalized * 0.1f, Space.Self);
        }

        if (verticalInput < 0)
        {
            transform.eulerAngles = new Vector3(0f, 180f, 0f);
            transform.Translate(movementy.normalized * 0.1f, Space.Self);
        }

        if (verticalInput > 0)
        {
            transform.eulerAngles = new Vector3(0f, 0f, 0f);
            transform.Translate(movementy.normalized * 0.1f, Space.Self);
        }
    }

Here's code attempt 2:

    public void playerMovement2()
    {
        Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
        movement = transform.TransformDirection(movement);
        transform.Translate(movement.normalized * 0.1f, Space.Self);
        transform.rotation = Quaternion.LookRotation(movement);
    }

I took your second attempt and modified it:

This way you will walk and look in the direction you

void Update()
{
    Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));

    transform.Translate(movement.normalized * 0.1f, Space.World);
    transform.rotation = Quaternion.LookRotation(movement, Vector3.up);

}

However then the character will always "flip" back to forward -> you can solve that by only moving when new input is given

For example like this:

void Update()
{
    Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));

    if (movement.magnitude > 0f)
    {
        transform.Translate(movement.normalized * 0.1f, Space.World);
        transform.rotation = Quaternion.LookRotation(movement, Vector3.up);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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