- Home /
Rotating a ship with joystick and keeping camera centered.
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;
void LateUpdate()
{
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, and similarily, when i let go of the right stick, it re-centers to point towards the global z axis, instead of it's parent's z axis. How do i fix this?