Rotate a GameObject

I have a game object that is controlled by the player, it can be moved in for directions: up, down, left and right, i want to change its rotation based on the direction it is moving, i have this code:

    public void move(Direction dir)
    {
        switch (dir)
        {
            case Direction.Left:
                body.velocity = new Vector2(-speed, 0);
                break;
            case Direction.Right:
                body.velocity = new Vector2(speed, 0);
                break;
            case Direction.Up:
                body.velocity = new Vector2(0, speed);
                break;
            case Direction.Down:
                body.velocity = new Vector2(0, -speed);
                break;
        }
        //rotate
        transform.rotation = Quaternion.LookRotation(body.velocity.normalized, Vector3.back);

        direction = dir;
        state = State.Walking;
    }

the problem is that the game object disappears after the rotation changes, can anyone help me?

LookRotation takes a forward vector, and an upward vector. Right now you’ve got the World Negative Z axis as your “up” vector. So your GameObject’s Z axis will point towards the velocity, and its Y axis will point at the negative World Z axis.

Is this intended? From the look of your code, this is a 2D character that probably doesn’t use the Z axis as forward or up.

I’m guessing the reason your object disappears is due to your camera’s viewing angle of the 2D object.

If you use the X axis as forward, you can do something like this:

// offset the velocity by 90 degrees to use as UP direction
Vector2 rotatedVector = Quaternion.AngleAxis(90, Vector3.back) * body.velocity.normalized;

transform.rotation = Quaternion.LookRotation(Vector3.forward, rotatedVector);

You could experiment with “transform.right = body.velocity” also.

Sorry for the delay, i was trying to figure out some things.

I read the documentation for the method LookRotation and they say that the z-axis will be aligned with the forward vector and the y-axis will be aligned with the up vector, in your answer you rotate the velocity by 90 degrees, it will be orthogonal to the x-axis(since the x-axis must point to the velocity) and then use the forward vector = (0, 0, 1), but i just realized that the sprite must be flipped when it moves in some direction, not just rotated by some angle around the z-axis, so i changed my method:

    public void move(Direction dir)
    {
        Vector2 speedOrtho = new Vector2();
        Vector3 forward = new Vector3();
        switch (dir)
        {
            case Direction.Left:
                body.velocity = new Vector2(-speed, 0);
                speedOrtho.Set(0, 1);
                forward.Set(0, 0, -1);
                break;
            case Direction.Right:
                body.velocity = new Vector2(speed, 0);
                speedOrtho.Set(0, 1);
                forward.Set(0, 0, 1);
                break;
            case Direction.Up:
                body.velocity = new Vector2(0, speed);
                speedOrtho.Set(-1, 0);
                forward.Set(0, 0, 1);
                break;
            case Direction.Down:
                body.velocity = new Vector2(0, -speed);
                speedOrtho.Set(1, 0);
                forward.Set(0, 0, 1);
                break;
        }
    
        transform.rotation = Quaternion.LookRotation(forward, speedOrtho);

        direction = dir;
        state = State.Walking;
    }

Now i calculate the forward and up vector based on the direction i’m moving, the up vector will always be orthogonal to the velocity vector since i want the x-axis to point in the moving direction, these two vector help me flip the sprite depending on the direction, for example when i’m moving to the left i must set the forward vector to be the z negative and the up vector will be the y-axis, now it appears to be working as i excepted, thanks for the helping.

I can be confusing things here because this are new concepts for me and i really think they are kinda confusing…

1 Like