Ship Tilting on Z Axis

Hey all,

I am working on a spaceship project and I am kinda stuck on something that I think should be fairly simple but can’t figure out a good way to do it. I am creating a Spaceship Game that is on a 2d plane(RigidBody frozen on Y Position) with a 3rd person Orbital Camera.

Since I am only worrying about speed and turning left and right I am just using these two lines for movement;

rb.AddRelativeTorque(new Vector3(0, Input.GetAxis(“Horizontal”) * turnRate * rb.mass, 0));
rb.AddRelativeForce(Vector3.forward * Speed);

Currently this works but it is fairly flat and boring so it would be nice to have a tilt effect, I know I want the ship to tilt 25 (left) or -25 (right) on the Z axis when using the Horizontal Axis, I didnt have any good results doing it to the direct transform so I tried rotating the child object (which holds the actual mesh model of the ship). The below code works slightly, except the model keeps pointing forward instead of turning with the AddRealativeTorque of the parent object.

After a few trying a few different things I figured Id best ask here instead of spinning my wheels for more hours :slight_smile: Would appreciate any assistance!

if (Input.GetAxis("Horizontal") > 0)
            {
                zAngle = Mathf.Lerp(zAngle, zAngleMin, speedRot * Time.deltaTime);
                PlayerShip.transform.rotation = Quaternion.Slerp(PlayerShip.transform.rotation, Quaternion.Euler(0, 0, zAngleMin), speedRot * Time.deltaTime);
            }
            else if (Input.GetAxis("Horizontal") < 0)
            {               
                zAngle = Mathf.Lerp(zAngle, zAngleMax, speedRot * Time.deltaTime);
                PlayerShip.transform.rotation = Quaternion.Slerp(PlayerShip.transform.rotation, Quaternion.Euler(0, 0, zAngleMax), speedRot * Time.deltaTime);
            }

Generally you will have the most success with these sorts of things by inserting a few “pivoting” GameObjects between the root object (which probably has the Rigidbody) and the visual, the spaceship.

The hierarchy would perhaps look like this:

SpaceShipRootGameObject
—PitchPivotGameObject
------RollPivotGameObject
----------RootOfVisibleSpaceShipMeshGameObject

Then you would adjust Pitch and Roll axes independently during (for example) acceleration or turning.

The scripting should probably be broken up into a few steps, roughly speaking:

  1. gather the input axes for both controls

  2. scale them according to thrust power and turning torque

  3. apply them to the rigidbody as you did above

  4. rescale the original input values gathered to use as pitch forward and roll left/right effect

  5. apply them to the interim pivots

That gives you ultimate control over all variables of spaceship performance and visible pitching/rolling.

1 Like

After some more frustrating hours trying to get this to work I scrapped this method and took my first venture into the Unity Animator system to accomplish this. A few more frustrating hours later I think I managed to put this together with animations. Doesn’t quite seem as smooth as if I did this via code but with some fine tuning of animation lengths I may just get this pulled off. On the plus side, I am finally learning the animation system.

1 Like

Also on the plus side, when you hire an animator, then the animator can make it perfect and you don’t have to ever touch it again.