Unity Mobile Course - Spaceship Rotation in wrong axis

I have followed the code exactly and I’m not sure why but I can’t get the ship to rotate on the correct axis.

I tried forward, up, down, back, left, and right. The rotation is always wrong.

When I tried moving the camera to a top-down perspective, it messed up the movement. Not really sure how to fix it. Some help would be appreciated.

Using Unity 2021.3

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] private float moveSpeed;
    [SerializeField] private float maxVelocity;
    [SerializeField] private float rotationSpeed;

    private Camera mainCamera;
    private Rigidbody rb;

    private Vector3 moveDirection;

    void Start()
    {
        mainCamera = Camera.main;
        rb = GetComponent<Rigidbody>();
    }
    
    void Update()
    {
        MyInput();
        KeepPlayerOnScreen();
        RotateOnMovement();
    }
    
    private void FixedUpdate()
    {
        if (moveDirection == Vector3.zero)
        {
            return;
        }
        else
        {
            MovePlayer();
        }
    }

    void MyInput()
    {
        try 
        {
            CalculateMoveDiretion();
        }
        catch (NullReferenceException)
        {
            return;
        }
    }

    void CalculateMoveDiretion()
    {
        if (Touchscreen.current.primaryTouch.press.isPressed)
        {
            Vector2 touchPosition = Touchscreen.current.primaryTouch.position.ReadValue();

            Vector3 worldPosition = mainCamera.ScreenToWorldPoint(touchPosition);

            moveDirection = worldPosition - transform.position; //Move to player's touch
            moveDirection.z = 0f; //stop from moving up and down
            moveDirection.Normalize(); //Just add a base fore, don't compound it
        }
        else
        {
            moveDirection = Vector3.zero;
        }
    }
    
    void MovePlayer()
    {
        rb.AddForce(moveDirection * moveSpeed, ForceMode.Force);

        rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxVelocity);
    }
    private void KeepPlayerOnScreen()
    {
        Vector3 newPos = transform.position;

        Vector3 viewPortPos = mainCamera.WorldToViewportPoint(transform.position);

        if (viewPortPos.x > 1)
        {
            newPos.x = -newPos.x + 0.1f;
        }
        else if (viewPortPos.x < 0)
        {
            newPos.x = -newPos.x -0.1f;
        }
        else if (viewPortPos.y > 1)
        {
            newPos.y = -newPos.y + 0.1f;
        }
        else if (viewPortPos.y < 0)
        {
            newPos.y = -newPos.y - 0.1f;
        }

        transform.position = newPos;
    }

    void RotateOnMovement()
    {
        if (rb.velocity == Vector3.zero) 
        { 
            return; 
        }
        else
        {

            Quaternion targetRotation = Quaternion.LookRotation(rb.velocity, Vector3.back);
            transform.rotation = Quaternion.Lerp
                (transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
        }
    }
}


To keep the vectors moving on the X,Y plane without insanity, it’s best to keep the camera looking forward, not top down…
This is the camera settings from the course:

This should work correctly with Vector3.back in the rotation code.

As you can see in my screenshot, my settings match those of the course exactly. I’m not sure why it behaves that way.

Apologies for the delay, I’ve been sick.

The next thing to check is the Player setup itself…

In the course repo (which you can clone/download from any of the This Lecture Changes links in the resources), the player is set up with the child object (StarSparrow) having a transform with location 0,0,0 and rotation 0,0,0.
The Player itself has a rotation of -90, 0, 0

I noticed in your OP, that the Player itself has a rotation of 0,0,0, which based on the model’s position relative to the camera in play mode infers that the model itself has a rotation of -90,0,0.

Try reversing these, so the model is 0,0,0 and the Player is -90, 0, 0

1 Like

Thanks for your reply. I know being sick sucks. I just recovered from a terrible cold last week.

You’re a legend man, thanks so much. I totally didn’t spot that mistake on my end. Now it works like a charm.

Have a great weekend!

I’m glad it’s sorted!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms