- Home /
How to rotate the camera with the player smoothly without the camera snapping back and forth?
So basically I got my player gameobject to move and rotate simple enough. then I was able to get the camera to follow the player with a slight delay. So I basically want to create the same delay effect for the rotation of the camera. I partially completed the coding and set the smoothTimeZ float in the inspector to 0.5 but there is a problem....... when the player's Z rotation goes between -1 to 1 the camera snaps back and forth. I believe it is due to the nature of the eulerAngle code I used but I'm just wondering how do I fixed this
Here is the codes so far.
public class FollowCamera2D : MonoBehaviour {
private Vector3 velocity;
public float smoothTimeY;
public float smoothTimeX;
public float smoothTimeZ;
public GameObject player;
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag("Player");
}
void FixedUpdate()
{
//Camera moving according to player position factoring in smoothtime
float posX = Mathf.SmoothDamp(transform.position.x, player.transform.position.x, ref velocity.x, smoothTimeX);
float posY = Mathf.SmoothDamp(transform.position.y, player.transform.position.y, ref velocity.y, smoothTimeY);
//Camera rotating according to player eulerangle
float rotZ = Mathf.SmoothDamp(transform.eulerAngles.z, player.transform.eulerAngles.z, ref velocity.z, smoothTimeZ);
transform.position = new Vector3(posX, posY, transform.position.z);
transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, rotZ);
}
Ugh I forgot to mention this camera is meant for Unity 2D
Answer by ImpOfThePerverse · Oct 05, 2017 at 04:45 PM
I've been using the following function to condition euler angles:
float PlusMinus180(float angle, float target)
{
while (angle > target + 180f)
{
angle -= 360f;
}
while (angle <= target - 180f)
{
angle += 360f;
}
return angle;
}
So your code would looks something like:
float zTemp = transform.eulerAngles.z;
float rotZ = Mathf.SmoothDamp(zTemp, PlusMinus180(player.transform.eulerAngles.z, zTemp), ref velocity.z, smoothTimeZ);
Answer by BlakeSchreurs · Oct 05, 2017 at 04:39 PM
Here's how I do it... (Note the misspelling of RotationSpeed, because I'm apparently classy like that). I create a temp. game object to mirror my camera's position and orientation, and then use LookAt to figure out what the ideal rotation should be. I then rotate at a given speed toward that ideal rotation. RotationSpeed is a public parameter so that I can tinker with it in the editor.
var go = new GameObject(); // a placeholder for the camera
go.transform.position = transform.position; // Place where camera is
go.transform.rotation = transform.rotation; // Rotate to how camera is rotated
go.transform.LookAt(GazeTarget); // Ideal look-at-target rotation
transform.rotation = Quaternion.Slerp(transform.rotation, go.transform.rotation, RotaitonSpeed * Time.deltaTime); // Spherical interploation of camera rotation
Destroy(go); // Get rid of the placeholder