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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Collision not registering from side of box collider

Discussion in 'Scripting' started by OOVUM, Oct 31, 2022.

  1. OOVUM

    OOVUM

    Joined:
    Mar 8, 2020
    Posts:
    17
    In my endless runner, my character and an incoming car both stop moving when they have a head-on collision. However, if I swerve my character into the side of the car while it's passing (like when your character gets dizzy in subway surfers), the player stops moving but the car doesn't detect the collision ( I checked with Debug.Log) and keeps on moving. My player uses a character controller and detects collision with OnControllerColliderHIt(). The car moves through Transform.Translate() and has a rigid body attached so it can use OnCollisionEnter(). I understand this might be a physics question, but was wondering if it might be a problem with OnCollisionEnter(). Does it only detect collision in the direction the object is moving? video attached and scripts for collision below, help is appreciated.
    FRONT ON COLLISION: https://thumbs.gfycat.com/CookedSecondaryIlladopsis-max-1mb.gif
    COLLISION FROM SIDE: https://thumbs.gfycat.com/NauticalWhisperedGrayfox-max-1mb.gif
    PLAYER COLLSION SCRIPT:
    private void OnControllerColliderHit(ControllerColliderHit hit)
    {
    if (hit.gameObject.name == "car_1")
    {
    touchingCar1 = true;
    transform.Translate(new Vector3(0, 0, 0));
    this.GetComponent<SwerveMovement>().enabled = false;
    }
    CAR COLLISION SCRIPT:
    private void OnCollisionEnter(Collision col)
    {
    if (col.gameObject.name == "Player")
    {
    transform.Translate(new Vector3(0, 0, 0));
    shouldMove = false;
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121

    ALSO: if you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  3. OOVUM

    OOVUM

    Joined:
    Mar 8, 2020
    Posts:
    17
    Thank you for the reply. Using MovePosition() did not fix the issue, however I will implement it if possible when the bug is fixed. Do you have any other ideas?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,006
    Well the character controller component isn't a rigidbody itself, instead using raycasts, etc, to prevent it from penetrating colliders.

    This is probably why the car doesn't receive collisions from it. The character collider will see that it 'collides' with the car using its own check methods, but probably never penetrates or actually hits the car's collider.

    Honestly I would forgo physics here and instead use trigger colliders instead.
     
    PraetorBlue likes this.
  5. OOVUM

    OOVUM

    Joined:
    Mar 8, 2020
    Posts:
    17
    In order to switch the collision to trigger colliders, would I have to switch my players character controller out since there is no trigger option for it, or can I continue to detect collision for the player via physics and a trigger collider for the car?
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,006
    Well I know the character controller can trigger... trigger colliders. The reverse I'm not sure, but you can of course put a trigger collider on the player as well.
     
  7. OOVUM

    OOVUM

    Joined:
    Mar 8, 2020
    Posts:
    17
    Thanks, I'm headed in the right direction since it detects collision from the side. However, a previous problem I had resurfaced - the car wont stop moving. Previously the car simply wasn't detecting the collision because I forgot to add a rigid body, but now even though the car is detecting the collision it wont stop.
    Code (CSharp):
    1. public class CarMovement : MonoBehaviour
    2. {
    3.     float speed = 5f;
    4.     bool shouldMove = true;
    5.  
    6.  
    7.  
    8.     private void Update()
    9.     {
    10.         if (shouldMove == true)
    11.         {
    12.  
    13.             transform.Translate(new Vector3(0, 0, speed) * Time.deltaTime);
    14.         }
    15.     }
    16.  
    17.  
    18.     void OnTriggerEnter(Collider other)
    19.     {
    20.         if (other.tag == "Player")
    21.         {
    22.             transform.Translate(new Vector3(0, 0, 0));
    23.             shouldMove = false;
    24.             Debug.Log("has collide");
    25.          
    26.         }
    27.     }
    28.  
    29.  
    30.  
    31.  
    32.  
    33. }
     
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,006
    Does the car still have a rigidbody? Is it's momentum just causing it to continue moving?

    Remember if you're using a rigidbody you should not be using normal transform manipulations. That Translate() call on line 22 is doing nothing as well.
     
  9. OOVUM

    OOVUM

    Joined:
    Mar 8, 2020
    Posts:
    17
    I removed the rigid body because if I am to use trigger colliders I believe it is not necessary, so I saw no point in keeping it on therefore used transform.Translate(). What else could it be?
     
    Last edited: Oct 31, 2022