Rotating a ship with joystick and centering the camera on the ship's foward

I have a spaceship that i want to move with the left joystick of my controller and two face buttons, and a camera as a child of the ship, set from the point of view of the pilot. The ship should rotate at a certain angle per second when i’m holding the left stick, and stop rotating when i let it go; and the camera should look around the cockpit with a certain degree of freedom when i move the right stick, and recenter to the middle of the cockpit when i let go.

The current codes i’m using are:

public class Movement:MonoBehaviour{
public int speed =1;
public float smooth =2.0F;
public float tiltAngle =30.0F;
void Update(){
transform.position += transform.forward * speed Time.deltaTime;
float pitch =Input.GetAxis(“Pitch”)
tiltAngle;
float yaw =Input.GetAxis(“Yaw”)* tiltAngle;
float roll =Input.GetAxis(“Roll”)* tiltAngle;
Quaternion target =Quaternion.Euler(pitch, yaw, roll);
transform.rotation =Quaternion.Slerp(transform.rotation, target,Time.deltaTime * smooth);
}
}

and

public class CameraController:MonoBehaviour{
public int maxAngleX;
public int maxAngleY;
public float camSmooth =2.0F;
voidLateUpdate()
{
float rotateY =Input.GetAxis(“CamHor”)* maxAngleX;
float rotateX =Input.GetAxis(“CamVert”)* maxAngleY;
Quaternion camTarget =Quaternion.Euler(rotateX,rotateY,0);
transform.rotation =Quaternion.Slerp(transform.rotation, camTarget,Time.deltaTime * camSmooth);
}
}

Now, with these scripts, the ship just changes angle while i hold the left stick, then returns to point towards the global Z axis when i let go, nd the camera still thinks the global foward is where it has to point. How can i fix these problems?

Well, first, you’re abusing Lerp (or Slerp) in both of your scripts. Read more about this.

Your problem in the second script is that you’re assigning to .rotation instead of .localRotation. It doesn’t do much good to be a child of the ship, if you ignore the parent rotation and assign a global rotation as you’re doing now.

The problem with the first one is kind of similar; you’re computing the target rotation de novo, based only on the inputs, so of course it goes back to neutral when the inputs go to zero. See if you can work out how to change that so that the inputs change your target, rather than setting it.

Alright, so on the cam script, i changed the last line for

transform.localRotation = Quaternion.Slerp(transform.localRotation, camTarget, Time.deltaTime * camSmooth);

and that fixed that.
Similarily, on the movement script, i declared yaw, pitch and roll outside of update, changed the code to

pitch += Input.GetAxis(“Pitch”) * tiltAngle;

and changed the last line to

transform.localRotation = Quaternion.Slerp(transform.localRotation, target, Time.deltaTime * smooth);

So now, tilting the left joystick up and down always tilts the ship up and down, pressing O or square always rolls the ship (barrel roll!) but holding the stick left or right when the ship is tilted up or down ALSO rolls!What is going on here?

–Edit: Oh, it’s because the ship has no parent.

1 Like

Cool. You’re still abusing Lerp, so your ship’s behavior is dependent on the frame rate and your path to true Unity with the universe is impeded, but at least you’ve got it working for now. Well done! :smile: