Monday, January 1, 2018

c# - How can I keep chase camera behind player when player rotates?


I have implemented a follow camera in Unity 3D. However, if the player object rotates, the camera does not rotate with it. To fix this I used RotateAround as follows:



playerRotation = CastPositive(player.EulerAngles.y) //guarantees 0-360
cameraRotation = CastPositive(transform.EulerAngles.y)
difference = playerRotation - cameraRotation
transform.RotateAround(player.position, Vector3.up, difference * time.deltaTime)

This works in general. However at some point the difference snaps from -50 to 210. When this happens, the camera swings in the opposite direction all the way round the player object, instead of continuing on its orbit.


How can I avoid this value jump?



  • I've tried clamping the various values to no avail, but maybe I've missed something?

  • Quaternions?




Answer



Code provided by Katherine Rix:


// Position camera to follow behind player's head.
private void Follow(){

// orientation as an angle when projected onto the XZ plane
// this functionality is modularise into a separate method because
// I use it elsewhere
float playerAngle = AngleOnXZPlane (player);

float cameraAngle = AngleOnXZPlane (transform);

// difference in orientations
float rotationDiff = Mathf.DeltaAngle(cameraAngle, playerAngle);

// rotate around target by time-sensitive difference between these angles
transform.RotateAround(GetTarget(), Vector3.up, rotationDiff * Time.deltaTime);
}

// Find the angle made when projecting the rotation onto the xz plane.

// You could pass in the rotation as a parameter instead of the transform.
private float AngleOnXZPlane(Transform item){

// get rotation as vector (relative to parent)
Vector3 direction = item.rotation * item.parent.forward;

// return angle in degrees when projected onto xz plane
return Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...