Sliding Physic, rigidbody jitter

Hello guys. I’m trying implement sliding physic but have tough times to make it jitter free. Here is scenario: i have one object → player with dynamic rigidbody, collider and another object → ground (also with collider). Both colliders have set physic material and rigidbody interpolation is set to interpolate. Ground is changing its rotation in smooth way and at this point i do not nothing with player at all in scripts. What is the problem that when rigidbody acquire higher velocity it start jittering. Also i have tried changing physic time step, velocity and position iterations but jittery movement still happened. Is there some workaround which help to solve my problem or should i just dont use body and calulate movements in script? Thanks.

How do you rotate your ground? If you use a simple Rotate() or setting transform.rotation this may “irritate” the physics system. Can you try to make the ground a rigidbody as well, with no gravity and rotate it using AddTorque()?

Ground have no rigidbody. I just randomly generate some rotation in random time intervals and slerp it. Code looks like this:

    void FixedUpdate()
    {
        currentTime += Time.deltaTime;

        //generate new angle and set reset current time
        if (currentTime > resetTime)
        {
            float newAngle = Random.Range(5f, -50f);
            resetTime = Random.Range(1.5f, 6f);
            speed = Mathf.Lerp(0.05f, 0.3f, (resetTime - 1.5f) / 4.5f);
            targetRotation = Quaternion.Euler(0f, 0f, newAngle);
            currentTime = 0;
        }

        // Smoothly rotate towards the target point.
        if (Mathf.Abs(transform.eulerAngles.z - targetRotation.z) > 0.1f)
        {
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
        }
    }

But as @Olipool said i am directly setting transform.rotation. The problem is that i have already tired not rotating it just made little slope to acquire sliding of player but with same result. When rigidbody acquire higher velocity it start jittering…

1 Like

Set variables in Update instead of FixedUpdate. Right now you are rotating the ground multiple times per frame. A lot of jitter can be prevented by calculating stuff once per frame, and acting upon those calculations in FixedUpdate.

You’re teleporting the ground instantly into colliders by directly modifying the Transform so it’s not surprising you’re having issues. Each update colliders find themselves overlapped. The ground isn’t rotating through space, it’s instantly appearing in a new position. The “jitter” as you describe it is probably this but it would’ve helped if you could’ve shown it because I see the word “jitter” used to describe pretty much anything that isn’t “smooth”.

Thanks a lot guys but as i’ve said, even without rotating the ground, player jittering occure. Here is video of my problem:

Video was captured from Windows stanalone build so it is not occuring only in editor. There are no scripts(controllers) on player or ground just colliders and rigidbody on player. Physics 2D, Time and other settings are set to default values.

Player setup: https://ibb.co/51q7n5v
Ground setup: https://ibb.co/qCyzqqt
Physics Material 2D setup: https://ibb.co/6m1f3nS

Only script is on camera which smoothly follow player. Here is the code:

[RequireComponent(typeof(Camera))]
public class SmoothCamera2D : MonoBehaviour
{
    public float dampTime = 0.01f;
    private Vector3 velocity = Vector3.zero;
    private Camera m_camera;
    public Transform target;

    private void Start()
    {
        m_camera = GetComponent<Camera>();
    }

    void LateUpdate()
    {
        if (target)
        {
            Vector3 point = m_camera.WorldToViewportPoint(target.position);
            Vector3 delta = target.position - m_camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z));
            Vector3 destination = transform.position + delta;
            transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
        }
    }
}

PS: sorry for links instead of images.

So now you’ve added a tracking camera to the problem. It’s best if you describe your issue fully rather than just adding stuff gradually. :wink:

Is the ball moving in the video (it’s not clear as there are no reference points) and I’ve no idea what the ground disappearing is about.

Are you sure this isn’t your follow camera script? I’m not sure why you’re using smooth-damp if this is updating per-frame or what the target is; the ball? I see you’re referring to a Transform but want to make you aware that an interpolating RB updates the transform per-frame with this on but the body isn’t at that position; it’s already at the position it moved to.

Maybe you’ve changed the physics settings to an extreme? Perhaps DefautlContactOffset?

Maybe share a repro project because it’d be far easier to look at it.

1 Like

@MelvMay sorry for blurry explanation of my problem. I’ve just realised from your reply that problem is in camera script so thanks a lot. I dont want camera to be fixed on player so for feel of follow camera i am using smooth dump (from docs: “Gradually changes a vector towards a desired goal over time.”).

1 Like

Although it looks a bit complicated at first, try Cinemachine. It’s in the packet manager and I found it amazing to work with. It’ll help you with a camera that smoothly follows your object

You should post on the DOTS physics forum here rather than hijacking an existing thread on the classic physics forum. Note that I have nothing to do with 3D DOTS physics either. Someone there should be able to help you though.

Sorry about that MelvMay!

No problem. The DOTS physics forum isn’t the most obvious forum to find so understandable.