Hi guys,
I need a camera that follows my ship. I found many tutorials but they don’t take rotation into account.
This is my script so far which doesn’t work at all [rotation is not affected at all, position does not stay behind]:
public class CameraScript : MonoBehaviour {
// Target related
public Transform Target;
// Self related
Camera cam;
Transform camTransform;
// Calculations
Quaternion lookAt;
[Header("Camera behaviour values")]
public float camRotationSpeed = 5.0f;
public float camFollowSpeed = 10.0f;
public Vector3 camOffset;
void Start () {
cam = gameObject.GetComponent<Camera>();
camTransform = cam.GetComponent<Transform>();
}
void LateUpdate () {
// Move the camera towards the target
camTransform.position = Vector3.Slerp(camTransform.position, Target.position + camOffset, Time.fixedDeltaTime * camFollowSpeed);
// Look at the target
lookAt = Quaternion.Slerp(camTransform.rotation, Target.rotation, Time.fixedDeltaTime * camRotationSpeed);
camTransform.rotation = lookAt;
}
}
What I like to achieve:
- Follow the player in a chase-cam style
- When the ship accelerates, the camera should lag behind a bit and eventually catch up [guess I need Slerp and take players Rigidbody velocity into account?]
- Always look at the player, but also smoothly
- Do not follow player rotations like a barrel roll, but if the player for example pitches the nose of the ship, slowly align the camera so the player can also look upwards.
Guess this is a more complex task but I have no real idea where to start and didn’t find helpful tutorials yet…