- Home /
smoothdamp causes jitters
I have a camera following a ball using the following code:
public float smoothTime = 0.3f;
private Vector3 velocity = Vector3.zero;
public Transform ball;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 target = new Vector3(ball.position.x, ball.position.y + 3, transform.position.z);
//transform.LookAt(ball);
transform.position = Vector3.SmoothDamp(transform.position, target, ref velocity, smoothTime);
}
Why do the objects that move in my scene jitter when i use the smoothdamp method? In the scene view it looks fine but in the game view it jitters a lot.
Answer by kag359six · Nov 24, 2012 at 03:46 PM
After further research I found the answer. The rigidbodies are not completely in snyc with the graphic updates so I had to put:
`body.interpolation = RigidbodyInterpolation.Interpolate;`
This makes the rigidbody in sync with the graphics, and prevents the jittery object effect when a camera smoothly follows it.
Just excactly did you do this? I'm sitting with the exact same problem as you. How'd you fix it? Where'd you put the interpolation code?
I put the code right after Rigidbody rb = GetComponent(); in my player controller. Solved the jitter problem for me!
Answer by darthbator · Nov 24, 2012 at 12:27 AM
Try placing the code inside the LateUpdate method. Normally when you are tracking moving objects in the scene you'll want to make sure it's done doing it's movement in the frame before you track them.
Also is there a reason you're using smoothDamp? If you're tracking the object at a constant distance you should be able to just adjust the camera's position relative to the target without having to smooth the value.
I use smoothDamp because i want the camera to slightly fall behind when the ball is moving ins$$anonymous$$d of it following its exact position all the time. And the LateUpdate didn't stop the jittering. Neither does Vector3.Lerp.
Answer by frankslater · Jul 10, 2017 at 08:20 PM
Keep in mind that Physics and frames run independently. If you move objects with physics (including FixedUpdate()), you need to move your camera with physics (and/or in FixedUpdate()) otherwise you will get jitter. If you move everything on a per frame basis, you need to move your camera the same way (in Update()). If by any reason you must use both physics and frame based movement in your scene, that's when you can look at interpolation, otherwise you are just tolling on performance with it.
Give this code a try:
public float smoothTime = 0.3f;
private Vector3 velocity = Vector3.zero;
public Transform ball;
void FixedUpdate () {
Vector3 target = new Vector3(ball.position.x, ball.position.y + 3, transform.position.z);
//transform.LookAt(ball);
transform.position = Vector3.SmoothDamp(transform.position, target, ref velocity, smoothTime);
}
Answer by dpami507 · Jul 24, 2020 at 03:37 AM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunPositionScript : MonoBehaviour
{
public Transform target;
public float posSpeed;
public float rotSpeed;
public Vector3 velocity = Vector3.zero;
void Start()
{
}
private void Update()
{
Sway();
}
// Update is called once per frame
void Sway()
{
gameObject.transform.position = Vector3.SlerpUnclamped(transform.position, target.transform.position, posSpeed * Time.deltaTime);
gameObject.transform.rotation = Quaternion.Slerp(transform.rotation, target.transform.rotation, rotSpeed * Time.deltaTime);
}
}
I Found That this creates a smooth follow and I hope this helps!