Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Rigidbody+collider goes through colliders when transform rotates

Discussion in '2D' started by Astrand, Sep 25, 2014.

  1. Astrand

    Astrand

    Joined:
    Sep 25, 2014
    Posts:
    3
    Basically, my character with a box collider, is inside a rotating room (clock-wise for example), each boundary has a box collider.


    If the transform for the room rotates too quickly, the character goes through goes through the boundaries regardless of their colliders. While researching I realized I need to use a raycast for colliding, but still I had problems. Found the DontGoThroughThings script and modify to 2D physics, but I am still having the same issue. Here is my modified script

    What am I doing wrong? How can I avoid 2D colliders from going through each other?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CustomCollider : MonoBehaviour
    5. {
    6.     public LayerMask layerMask; //make sure we aren't in this layer
    7.     public float skinWidth = 0.1f; //probably doesn't need to be changed
    8.    
    9.     private float minimumExtent;
    10.     private float partialExtent;
    11.     private float sqrMinimumExtent;
    12.     private Vector2 previousPosition;
    13.     private Rigidbody2D myRigidbody;
    14.     public GUIText debugText;
    15.  
    16.  
    17.     void Start () {
    18.         debugText.text = "";
    19.     }
    20.     //initialize values
    21.     void Awake()
    22.     {
    23.         myRigidbody = rigidbody2D;
    24.         previousPosition = myRigidbody.position;
    25.         minimumExtent = Mathf.Min(Mathf.Min(collider2D.bounds.extents.x, collider2D.bounds.extents.y));
    26.         partialExtent = minimumExtent * (1.0f - skinWidth);
    27.         sqrMinimumExtent = minimumExtent * minimumExtent;
    28.     }
    29.    
    30.     void LateUpdate()
    31.     {
    32.         //have we moved more than our minimum extent?
    33.         Vector2 movementThisStep = myRigidbody.position - previousPosition;
    34.         float movementSqrMagnitude = movementThisStep.sqrMagnitude;
    35.        
    36.         if (movementSqrMagnitude > sqrMinimumExtent)
    37.         {
    38.             float movementMagnitude = Mathf.Sqrt(movementSqrMagnitude);
    39.             RaycastHit2D hit = Physics2D.Raycast(previousPosition, movementThisStep, movementMagnitude, layerMask.value);
    40.            
    41.             //check for obstructions we might have missed
    42.             if (hit.collider != null)
    43.                 myRigidbody.position = hit.point - (movementThisStep/movementMagnitude)*partialExtent;
    44.         }
    45.        
    46.         previousPosition = myRigidbody.position;
    47.         debugText.text = "Position: " + myRigidbody.position;
    48.     }
    49. }
    50.  
     
  2. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Is the room rotating with Rigidbody2D.MoveRotation or Rigidbody2D.angularVelocity? If you're setting the rotation any other way (for example, modifying the transform), you're asking for trouble; the movement will not be physics compliant. Also, have you tried setting the collisionDetectionMode to continuous?
     
    Astrand likes this.
  3. Astrand

    Astrand

    Joined:
    Sep 25, 2014
    Posts:
    3
    The room is indeed rotating modifying the transform. Let me try to use Rigidbody methods to move the room. Thanks for the reply!
     
  4. Astrand

    Astrand

    Joined:
    Sep 25, 2014
    Posts:
    3
    Well I used Rigidbody2D.MoveRotation to do the rotation, also the collisiondetectionmode is continous. After certain speed the character goes through one of the walls/floors

    Same with angular velocity :(
     
    Last edited: Sep 25, 2014