Spaceship movement - answers not working

Hello,
Im making a space game, where the ship can only travel in 3 lanes (left, middle, right - like an endless runner game), my question is how do i rotate the ship while moving in lanes? Like from Left to middle it should rotate on Z by -40 and when arrived it should be on 0 again. Can some1 fill up my script with the rotation part? Or suggest anything that could help? Thank you for your time and sorry for posting this here, i coudnt make the answer section work.

Here is my movement script:

private const float LANE_DISTANCE = 20f;
public float speed = 7.0f;
private float gravity = 12f;

private CharacterController controller;
private float verticalVelocity;
private int desiredLane = 1;  // 0 = Bal, 1 = Közép, 2 = Jobb

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

private void FixedUpdate()
{
     // Mozgás balra        
     if (Input.GetKeyDown(KeyCode.A))
         MoveLane(false);
         // Mozgás jobbra
     if (Input.GetKeyDown(KeyCode.D))
         MoveLane(true);

     // Kiszámolja, hogy a JÖVŐBEN hol kell lennünk.

     Vector3 targetPosition = transform.position.z * Vector3.forward;
     if (desiredLane == 0)
         targetPosition += Vector3.left * LANE_DISTANCE;
     else if (desiredLane == 2)
         targetPosition += Vector3.right * LANE_DISTANCE;

     // Move delta

     Vector3 moveVector = Vector3.zero;
     moveVector.x = (targetPosition - transform.position).normalized.x * speed;

     // Calculate Y
     if (IsGrounded())
     {
         verticalVelocity = -0.1f;
     }

     moveVector.y = -0.1f;
     moveVector.z = speed;

     // Űrhajó controller
     controller.Move(moveVector * Time.deltaTime);
}

private void MoveLane(bool goingRight)
{
     desiredLane += (goingRight) ? 1 : -1;
     desiredLane = Mathf.Clamp(desiredLane, 0, 2);
  
}
private bool IsGrounded()
{
     Ray GroundRay = new Ray(
        new Vector3(
            controller.bounds.center.x,
            (controller.bounds.center.y - controller.bounds.extents.y) + 0.2f,
            controller.bounds.center.z),
        Vector3.down);
     return false;
}
}

You could create an animation an call that, or even a tween. Alternately you set a rotation value / target and smooth damp to that value. Setting it back to zero when you get to the lane.

1 Like

Thank you so much for your answer! I just started programming and using unity, im a total beginner in everything. Tried to fix it with animation as you mentioned, but it didnt really work because its somehow overwrote the movement script, and the ship didnt move or lagged while moving. Im pretty sure its my fault tho, because i dont know animation either, probably did something wrong. Thats why i wanted to fill this code with the rotation part, but coudnt figure it out yet. I dont know about tween, ill look it up! Thank you again for your time and help!