Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Rigidbody with Collider partialy passing throught Box Collider

Discussion in 'Physics' started by mavener, Jul 10, 2020.

  1. mavener

    mavener

    Joined:
    May 23, 2020
    Posts:
    2
    Hi there,
    I'm trying to understand how rigidbodies and colliders work. I was expecting my object to be stopped by the collider (without entering the collider) but instead it keeps entering and exiting the collider as seen here:



    - I tried moving the code to FixedUpdate but it didn't help.
    - I tried AddForce, it seems to work but if the force is big enough, it enters the collider too and bounces back.
    - Tried to change the Collision Detection to all 4 modes. All the same.

    What am I doing wrong? Or is it expected behavior, and I have to somehow calculate next frame collision in advance and stop the movement in code?

    The "Player" object has a Rigidbody with Box Collider - without gravity and Constrained rotation.
    The "Wall" is a basic Cube with Box Collider.

    Code (CSharp):
    1. public class movement : MonoBehaviour
    2. {
    3.     private float forward;
    4.     private Rigidbody rb;
    5.     private void Awake()
    6.     {
    7.         rb = GetComponent<Rigidbody>();
    8.     }
    9.  
    10.     void Update()
    11.     {
    12.         forward = Input.GetAxis("Vertical");
    13.         Vector3 movement = transform.forward * forward * 25 * Time.deltaTime;
    14.         rb.MovePosition(transform.position + movement);
    15.     }
    16.  
    17.     private void OnGUI()
    18.     {
    19.         GUI.Box(new Rect(10, 10, 150, 25), $"forward: {forward:0.##}");
    20.     }
    21. }