Rigidbody Jittery Collision

Hello!


I am a Unity beginner / intermediate trying to set up a very basic player controller using a Rigidbody. However, when testing the controller, collision against any obstacle results in a very jittery behavior, as if the object was being placed inside of the object and rocketed back out. Described behavior is shown below in a gif, alongside my game objects’ settings and code. I am using MovePosition() on the Rigidbody to generate the movement, and the Input System package from Unity to control player input.


I’ve been looking around for a few hours now, messing with various settings, and the common fixes and forum posts don’t seem to be working. Here are a few things I have tried:

  1. Difference between Update() and FixedUpdate(). The movement calculations are done in Update(), and the application of them / physics based items are done in FixedUpdate().
  2. Checking interpolation and collision detection methods under the Rigidbody. I have tried every combination of both (for example, Continuous + Interpolate, Discrete + None, etc.).
  3. Make sure “IsKinematic” is false. It is (I have also tried it as true).
  4. Apply frictionless Physic material to collision (I have tried with the material both on and off the object).
  5. Don’t modify the position of the object in any way except through Rigidbody modifications.

Everything I am doing should be very simple, and stuff I’ve done before with no problems. Here is the exact path I took to recreate this problem, if it helps:

  1. Create new empty game object called “Player”
  2. Add “PlayerController.cs” script I made to this new game object (code given below) and set “Speed” to 10.
  3. Add a PlayerInput component from the input system package. Drag in my custom input actions asset, set the behavior to invoke Unity events, drag my script into the object space for the “Movement” input action (a simple Vector2 input axis bound to WASD) and choose my OnMovement() function to subscribe to the event.
  4. Add a Capsule Collider component; leave it with default settings.
  5. Add a Rigidbody component. Change “Interpolate” to “Interpolate”, “Collision Detection” to “Continuous”, and freeze the rotation on the X, Y, and Z axes.
  6. Add a 3dObject → Cube child to “Player”; remove the box collider.
  7. Add two 3dObject → Cube to the scene as a ground and floor.
  8. Test it out, and see unfortunate behavior :frowning:

I’ve been banging my head against the wall for a good bit now. I suppose I could do some sort of Raycast to manually check for collisions, but I’d much rather get it working this way. If you need any further clarifications, screenshots, or descriptions of behavior, just let me know and I’ll be happy to provide them. Any help would be greatly appreciated. Thank you in advance!


Gif of the undesired behavior:
Click me!!!
Player game object inspector screenshot:


205559-screenshot-20230314-063705.png
Code of undesired behavior:

using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{

    // Variables
    public float speed;
    private Rigidbody rb;
    private Vector2 moveInputAxes;
    private Vector3 moveAmt;

    // Start, set reference to the Rigidbody component
    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    // Update, calculate movement
    private void Update()
    {
        moveAmt = new Vector3(moveInputAxes.x, 0, moveInputAxes.y) * speed * Time.deltaTime;
    }

    // FixedUpdate, apply movement
    private void FixedUpdate()
    {
        rb.MovePosition(rb.position + moveAmt);
    }

    // PlayerInput callback, read value for use in Update
    public void OnMovement(InputAction.CallbackContext value)
    {
        moveInputAxes = value.ReadValue<Vector2>();
    }

}

you are using rb.MovePosition? I have not seen anyone do that but should not be a problem, try changing the collison detection mode to continuous dynamic, has fixed many things for me