admin管理员组

文章数量:1417407

My player can ride a bike, and the idea is for it to turn to the direction the player is moving rather than snapping into the direction. The player moves with W,A,S,D.

The bike turns, but it's always facing -90 degrees in any direction it is going, so for example, if my player is moving forward/north (camera doesn't turn) the bike is facing left/west.

How can I fix this so if I'm going forward the bike looks forward, and turns smoothly between directions.

private void Update()
{
    HandleInput();
    MoveBike();
    RotateObject();
}

private void HandleInput()
{
    movementDirection = Vector3.zero;

    if (Input.GetKey(KeyCode.W))
    {
        movementDirection += Vector3.forward;
    }
    if (Input.GetKey(KeyCode.S))
    {
        movementDirection += Vector3.back;
    }
    if (Input.GetKey(KeyCode.A))
    {
        movementDirection += Vector3.left;
    }
    if (Input.GetKey(KeyCode.D))
    {
        movementDirection += Vector3.right;
    }

    movementDirection = movementDirection.normalized;
}

private void MoveBike()
{
    transform.position += movementDirection * speed * Time.deltaTime;
}

private void RotateObject()
{
    if(movementDirection != Vector3.zero)
    {
        Quaternion targetRotation = Quaternion.LookRotation(movementDirection);
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
    }
}

My player can ride a bike, and the idea is for it to turn to the direction the player is moving rather than snapping into the direction. The player moves with W,A,S,D.

The bike turns, but it's always facing -90 degrees in any direction it is going, so for example, if my player is moving forward/north (camera doesn't turn) the bike is facing left/west.

How can I fix this so if I'm going forward the bike looks forward, and turns smoothly between directions.

private void Update()
{
    HandleInput();
    MoveBike();
    RotateObject();
}

private void HandleInput()
{
    movementDirection = Vector3.zero;

    if (Input.GetKey(KeyCode.W))
    {
        movementDirection += Vector3.forward;
    }
    if (Input.GetKey(KeyCode.S))
    {
        movementDirection += Vector3.back;
    }
    if (Input.GetKey(KeyCode.A))
    {
        movementDirection += Vector3.left;
    }
    if (Input.GetKey(KeyCode.D))
    {
        movementDirection += Vector3.right;
    }

    movementDirection = movementDirection.normalized;
}

private void MoveBike()
{
    transform.position += movementDirection * speed * Time.deltaTime;
}

private void RotateObject()
{
    if(movementDirection != Vector3.zero)
    {
        Quaternion targetRotation = Quaternion.LookRotation(movementDirection);
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
    }
}
Share Improve this question asked Jan 31 at 10:53 EPiedrahitaEPiedrahita 394 bronze badges 3
  • 2 Is it possible that just your visuals (3D model etc) are simply rotated against this root object? – derHugo Commented Jan 31 at 13:56
  • You need a starting point (A) and an end point (B); and a starting "facing" angle and a target facing angle (difference == the sweep) .... for the "turn". You can move from A to B while turning (rotating) the "sweep". The duration of the "rotation" is the same as the time to move from A to B (rotates faster when moving faster). – Gerry Schmitz Commented Jan 31 at 19:50
  • Think about it. A "turn" has a starting point and an end point. If "we" can't agree on that, you won't understand the rest either. I'm talking "solutions"; not "why" his code "doesn't work"; which isn't a solution. I was expecting this moment. – Gerry Schmitz Commented Feb 1 at 18:55
Add a comment  | 

1 Answer 1

Reset to default 0

To get angle use Mathf.Atan2 function instead. Here's code to improve rotation of object

private void RotateObject()
{
    if (movementDirection != Vector3.zero)
    {
        //That's for 3D. Use x and y for 2D
        float angle = Mathf.Atan2(movementDirection.z, movementDirection.x) * Mathf.Rad2Deg;
        //Get quaternion by angle. Rotation by z axis. For other change Vector.forward to another
        Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
        //Rotate object
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
    }
}

That may not work properly to you because it might rotate by unwanted axis or wrong angle. Edit code values to fit your requirements

本文标签: cGame Object facing the wrong direction when movingStack Overflow