- Home /
Screen jitters when looking around as the Rigidbody moves
I have a basic first person character with a simple script that allows movement and looking around with the mouse. The problem occurs when moving around AND looking around. The screen jitters and the edges of objects become shaky. If I am only moving but not using the mouse there is no problem. Likewise, if I am looking around but not moving there is no issue. Here is the code I am using on the character:
public class PlayerControl : MonoBehaviour {
public float mSpeed;
public float lSpeed;
public Rigidbody rb;
public GameObject cam;
// Use this for initialization
void Start () {
QualitySettings.vSyncCount = 0;
}
// Update is called once per frame
void Update () {
looking();
}
void FixedUpdate()
{
movement();
}
void movement()
{
float moveHor = Input.GetAxis("Horizontal");
float moveVert = Input.GetAxis("Vertical");
rb.AddRelativeForce(moveHor * mSpeed, 0, moveVert * mSpeed);
}
void looking()
{
float lookHor = Input.GetAxis("Mouse X");
float lookVert = Input.GetAxis("Mouse Y");
transform.Rotate(0, lookHor * lSpeed, 0);
cam.transform.Rotate(-lookVert * lSpeed, 0, 0);
}
}
Thank you for the help
[1]: /storage/temp/90247-desktop-03192017-11165005.png
You shouldn't use any kind of AddForce for movement, use velocity. Also for both your Looking and $$anonymous$$oving scripts, they should both be called from FixedUpdate. Also, you're look and movement should be scaled by Time.fixedDeltaTime.
I moved both methods into the Fixed Update and still get jittering. How can I use velocity for movement ins$$anonymous$$d of Addforce while being scaled by Time.deltaTime? Thanks for the help
Answer by Commoble · Mar 19, 2017 at 07:58 PM
Your camera movement should be in the same update cycle as the object it's following. Update and FixedUpdate occur at different rates relative to each other, and this ratio of updates-per-second isn't constant, and having a camera moving in Update following something that moves via physics or FixedUpdate can cause jittering.
Still jitters if both are in the same update cycle. RobAnthem mentioned using velocity ins$$anonymous$$d of AddForce but I am not sure how to do that and scale it by time.DeltaTime. Thanks for the help
Answer by RobAnthem · Mar 21, 2017 at 02:01 AM
By moving and object with AddForce, it will automatically gain jitters if it is not a single force event. The inertia of the force slows down each frame, but then force gets added back in. This causes the entity to get launched forward even faster, and then the inertia tries to slow back down. Then compound this with Update and things get "jittery" FixedUpdate is called a fixed number of frames. Like if we were to basically create a system timer and use it for our own callbacks, we could make a custom "Update" method. The variance in the amount of time from Update and FixedUpdate is that Fixed Update occurs at intervals, Update occurs again the moment the previous update is complete.
So we use velocity, scaled over a fixed period of time. Creating more consistency. Moving something by Velocity is as simple as
public Rigidbody rigidbody;
public float speed;
private float vertical;
void FixedUpdate()
{
vertical = Input.GetAxis("Vertical");
rigidbody.velocity = (transform.forward * vertical) * speed * Time.fixedDeltaTime;
}
For strafing
void FixedUpdate()
{
vertical = Input.GetAxis("Vertical");
horizontal = Input.GetAxis("Horizontal")
rigidbody.velocity = ((transform.forward * vertical) + (transform.right* horizontal)) * speed) * Time.fixedDeltaTime;
}
To scale it over time, here's a mock example, plenty of ways to do it though.
private float velocity;
public float maxVelocity;
public float acceleration;
void FixedUpdate()
{
vertical = Input.GetAxis("Vertical");
horizontal = Input.GetAxis("Horizontal")
if (vertical != 0 || horizontal != 0)
{
if (velocity <= maxVelocity)
{
velocity += acceleration * Time.fixedDeltaTime;
}
}
else if (velocity > 0)
{
velocity -= acceleration * Time.deltaTime;
if (velocity < 0)
velocity = 0;
}
rigidbody.velocity = ((transform.forward * vertical) + (transform.right* horizontal)) * velocity) * Time.fixedDeltaTime;
}
Glad to help, if this was the answer, go ahead and mark it as answered.
Your answer
Follow this Question
Related Questions
What is the best way to move a character for an FPS? 1 Answer
Player not following touch after camera is rotated? 0 Answers
Camera defaulting to -90 Y 0 Answers
only rotate on x axis 1 Answer
3D RTS camera 3 Answers