Search code examples
c#unity-game-enginemonodevelopgame-physicsgame-development

ship moves position on z axis when trying to tilt it while turning?


I am making a 2.5D game in unity which is about a spaceship traveling through space more like traditional spaceship games.

I want my spaceship to tilt a bit when the player turns like planes do, like this plane is turning:

LIKE THIS PLANE IS TURNING

This is what my screen ship looks while taking a turn:

This is what my screen ship looks while taking a turn

I want it to tilt a little bit, this is my moment code right now

    void Turn()
    {
        float RotationCount = Input.GetAxis("Horizontal");
        float TurnShip = turnSpeed * Time.deltaTime * Input.GetAxis("Horizontal");
        if (Input.GetAxis("Horizontal") < 0)
            ForwardTransform.Rotate(0, TurnShip,10*Time.deltatime)
        else if (Input.GetAxis("Horizontal") > 0)
            ForwardTransform.Rotate(0, TurnShip,-10*Time.deltatime);


        //if (RotationCount < 0)
        //    myT.Rotate(0, TurnShip, 9);
        //else if (RotationCount > 0)
        //    myT.Rotate(0, TurnShip, -9);
    }

This is rotation script and below is thurst scrip

    void Thrust()
    {

        if (Input.GetAxis("Vertical") > 0.75f)
        {
            V = Input.GetAxis("Vertical");
        }


        myT.position += myT.forward * movementSpeed * Time.deltaTime * V;
    }

But when i use this code the ship changes its position on Z-Axis as the nose of ship turns down or up depending on movement

how do i tilt the ship without changing position on Z-Axis

Ships initial Position:

Ships initial Position

When user turns the ship:

When user turns the ship

What other way should I be turning my ship?


Solution

  • Why not have an intermediate object and make the spaceship a child of that?

    So instead of rotating the ship, you would rotate this intermediate object, and then you can move the ship base on the movement. The trick here is that the intermediate object only rotates in one axis, and the ship only ever rotates (relative to its parent transform) in one axis.

    This will avoid any Euler angle/Quaternion/moving reference frame business, which for such a simple problem you should need.