Jittery movement of the camera when rotating AND moving - 2D top down
Hello all,
I am making a 2d top down game where the player can rotate the camera around himself as he moves through the world.
Everything works fine when the player is moving around the world the camera follows him fine. if he standing still then the camera rotation also works very well. However as soon as I start to do both then there is a jittery in the camera that makes all other objects jitter besides the player.
Now in working with this problem I have found out that this could have to do with the fact that I use rigidbody2d.AddRelativeForce (so physics) to move the player and that his movements are checked in FixedUpdate.
https://forum.unity3d.com/threads/camera-jitter-problem.115224/ http://answers.unity3d.com/questions/381317/camera-rotation-jitterness-lookat.html
I have tried moving my camera rotation and the following scripts to LateUpdate, FixedUpdate, Update you name it. Nothing seems to work. I am sure that there is some sort of delay between movement of the camera and the rotation that is causing this. I am wondering if anyone has any feedback?
Scripts:
To Follow character, script applied to a gameobject that has the camera as a child
public class FollowPlayer : MonoBehaviour {
public Transform lookAt;
public Spawner spawner;
private Transform trans;
public float cameraRot = 3;
private bool smooth = false;
private float smoothSpeed = 30f;
private Vector3 offset = new Vector3(0, 0, -6.5f);
//test
public bool changeUpdate;
private void Start()
{
trans = GetComponent<Transform>();
}
private void FixedUpdate()
{
CameraRotation();
}
private void LateUpdate()
{
following();
}
public void following()
{
Vector3 desiredPosition = lookAt.transform.position + offset;
if (smooth)
{
transform.position = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
}
else
{
transform.position = desiredPosition;
}
}
void CameraRotation()
{
if (Input.GetKey("q"))
{
transform.Rotate(0, 0, cameraRot);
}
if (Input.GetKey("e"))
{
transform.Rotate(0, 0, -cameraRot);
}
}
public void SetTarget(string e)
{
lookAt = GameObject.Find(e).GetComponent<Transform>();
}
}
The character Controller, script applied to the Player, is called in FixedUpdate
private void HandleMovement()
{
if (Input.GetKey("w"))
{
rigid.AddRelativeForce(Vector2.up * speed);
}
if (Input.GetKey("s"))
{
rigid.AddRelativeForce(Vector2.down * speed);
}
if (Input.GetKey("a"))
{
if (facingRight)
{
Flip();
}
rigid.AddRelativeForce(Vector2.left * speed);
}
if (Input.GetKey("d"))
{
if (!facingRight)
{
Flip();
}
rigid.AddRelativeForce(new Vector2(1,0) * speed);
}
}
Any Input is appreciated.
Thank you.
Answer by SteenPetersen · May 10, 2017 at 02:15 PM
I fixed this problem finally:
First I made my cameraRotation() and follow() into 1 function and gave it a better clearer name:
public void UpdateCameraPosition()
{
if (Input.GetKey("q"))
{
transform.Rotate(0, 0, cameraRot);
}
else if (Input.GetKey("e"))
{
transform.Rotate(0, 0, -cameraRot);
}
Vector3 desiredPosition = lookAt.transform.position + offset;
transform.position = desiredPosition;
}
I then called that function from the character controller straight after the handling of movement. Both calls (handleMovement & cameraPosition) in fixedUpdate.
void FixedUpdate()
{
HandleMovement();
cameraController.UpdateCameraPosition();
}
and the jittering was gone.
So it seems it was imply because there was a slight delay between the two calls as the previous posts I read had said. But I had failed to be able to properly Set them close enough together.
Hope this helps someone.
Answer by IgorAherne · Jun 29, 2018 at 04:31 PM
Searched the web and everybody is talking about FixedUpdate or LateUpdate which doesn't help me either. And I don't want any smooth lerping! :D
If I reposition my camera, and then make it look at my character, it will jitter. This is related to unity's order of building MVP matrix for the camera.
Literally, nobody mentioned this SO I AM GOING TO PUT MY SOLUTION IN BOLD. You have to sacrifice 1 frame of your camera, and set it to the previous lookAt position:
Transform myLookTarget; //follow your gaze after this object
Vector3 prevPos;
void MyUpdate(){
camera.transform.position = someSampledPosition;
camera.transform.rotation = Quanternion.Lookat(previousPos);
previousPos = myLookTarget.transform.position; //update AT THE END of the function
}
Answer by True_Beef · Aug 11, 2018 at 03:50 PM
@IgorAgerne Nicely put.
Sorry to rev an old thread, but I also want to interject that using RotateTowards DOES NOT WORK for cam control. This method uses degrees of control meaning the faster you want it to go, the bigger the steps it will take, causing jitter. If you want to modify rotation directly (without using a vector to look at) Use a form of Slerping after positioning everything to make it look like butter. (All in fixed update, mind you.)
Your answer
Follow this Question
Related Questions
How do I get the current active camera? 2 Answers
Rotate towards mouse pointer 1 Answer
Extremely Specific 2D camera rotation problem 0 Answers
URP: use multiple render data together 0 Answers