Search Unity

Inaccuracy in distance given by Ridgidbody2D.Cast?

Discussion in '2D' started by Drewtooroo, Nov 3, 2018.

  1. Drewtooroo

    Drewtooroo

    Joined:
    Jul 12, 2017
    Posts:
    4
    I'm trying to move a 2D physics object using Ridgidbody2D.Cast. When a collision is detected, the distance given by the RaycastHit2D seems incorrect because the collider never ends up flush with the collider that was hit.

    Screen Shot 2018-11-03 at 11.25.28 AM.png

    I've made a sample project. If you hit play and watch both colliders in the scene view of the editor, you can see that the CircleCollider2D of the falling "ball" object goes a little ways into the BoxCollider2D of the "Quad" object.

    https://github.com/drewgingerich/Raycast2D-Overshoot-Test

    Here's the script that handles movement:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(Rigidbody2D))]
    6. [RequireComponent(typeof(Collider2D))]
    7. public class Actor : MonoBehaviour {
    8.  
    9.     [SerializeField]
    10.     private Collider2D otherCollider;
    11.  
    12.     private Rigidbody2D rb2d;
    13.     private Collider2D collider;
    14.     private RaycastHit2D[] hitBuffer = new RaycastHit2D[8];
    15.     private ContactFilter2D contactFilter;
    16.    
    17.     void Awake() {
    18.         rb2d = GetComponent<Rigidbody2D>();
    19.         collider = GetComponent<Collider2D>();
    20.  
    21.         contactFilter = new ContactFilter2D();
    22.         contactFilter.useTriggers = false;
    23.         contactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(gameObject.layer));
    24.     }
    25.  
    26.     public void Move(Vector2 move) {
    27.         float distance = move.magnitude;
    28.         Vector2 direction = move.normalized;
    29.         float searchDistance = distance;
    30.  
    31.         int count = rb2d.Cast(direction, hitBuffer, searchDistance);
    32.         if (count > 0) {
    33.             distance = hitBuffer[0].distance;
    34.         }
    35.  
    36.         // transform.Translate((Vector3)direction * distance);
    37.         rb2d.MovePosition(rb2d.position + direction * distance);
    38.  
    39.         ColliderDistance2D colDistance = collider.Distance(otherCollider);
    40.         Debug.Log(string.Format("Cast distance: {0}, Overlap distance: {1}", distance, colDistance.distance));
    41.     }
    42. }
    And here's the script that causes downward movement using the above script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(Actor))]
    6. public class SimpleBallController : MonoBehaviour {
    7.  
    8.     private Actor actor;
    9.  
    10.     void Awake() {
    11.         actor = GetComponent<Actor>();
    12.     }
    13.  
    14.     void FixedUpdate() {
    15.         actor.Move(Vector2.down * Time.fixedDeltaTime);
    16.     }
    17. }
    18.  
     
    Deleted User likes this.
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,478
    Hmm, that is very odd. I'll take a look for you in a little while.
     
  3. Drewtooroo

    Drewtooroo

    Joined:
    Jul 12, 2017
    Posts:
    4
    Thank you! I also posted this question to Unity Answers here. I have not received any solutions and I'll try to keep the information in parallel, but I figure it's good to have everything in plain view. And I'll do my best to provide more information if you need it!
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,478
    I can see the answers you were provided but they are not exactly correct. It seems there is indeed a bug with circle vs anything else in that it slightly overshoots by the contact offset. This is most certainly a bug.

    Is there any chance you could provide a bug report I can fix this against? Do not worry if not but it would certainly help verification.
     
  5. Drewtooroo

    Drewtooroo

    Joined:
    Jul 12, 2017
    Posts:
    4
    I just submitted a bug report: case 1098169. Thanks!
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,478
    Thanks, I have the case and will look at.
     
    Drewtooroo likes this.
  7. Drewtooroo

    Drewtooroo

    Joined:
    Jul 12, 2017
    Posts:
    4
    Hey there, any word on this issue? Should I just be waiting to get an email update about the bug report? It's kinda stalled my project since it seems so fundamental to the type of character controller I'm trying to make. Again, thanks for your help!