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

Bug Rigidbody not colliding if use gravity is turned off

Discussion in 'Physics' started by docFarto, Jul 28, 2023.

  1. docFarto

    docFarto

    Joined:
    Mar 28, 2022
    Posts:
    2
    So I am very confused by this, but when I turn off gravity OnCollisionEnter on my target object does not trigger.
    I have a very simple example script:
    Code (CSharp):
    1.     private void OnCollisionEnter(Collision collision)
    2.     {
    3.         Debug.Log("collided");
    4.     }
    The target has a sphere collider and a rigidbody

    In my game you shoot a bullet (that has a sphere collider on and no rigidbody) that has this code:
    Code (CSharp):
    1.     public void FixedUpdate() {
    2.             transform.Translate(transform.forward * Time.smoothDeltaTime * speed, Space.World);
    3.     }
    I have all the transforms frozen on the target object, I am only using the rigidbody to check for a collision via code.
    It works every time when Use Gravity is checked. But when I uncheck Use Gravity, it only triggers the collision every third time or so, I have tried changing the Collision detection to Dynamic or continuous Dynamic, but that doesn't change anything.
    I am looking in the scene view and they clearly collide, the speed of the bullet is also at 1 and very slow.
    I have tried with isKinematic checked and unchecked and to no avail.

    Does anyone have any ideas how Use Gravity might be affecting my collision detection? Any thoughts would be helpful.

    I have tried to attach a picture of what I am think should work, the colTest script is the first script I have posted in the thread.
     

    Attached Files:

  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Moving objects need to have a rigidbody on them and should be moved with physics functions like Rigidbody.AddForce. Enabling gravity makes your object more active and so it checks for collisions against static objects more frequently. But if you wait longer you may find that the bullet never triggers a collision at all because the rigidbody has gone to sleep and has stopped checking for collisions completely. Why check for collisions if nothing is moving?.

    To the physics engine transform.Translate doesn't register as moving.. transform.anything doesn't register as moving.
     
  3. docFarto

    docFarto

    Joined:
    Mar 28, 2022
    Posts:
    2
    Oh that makes sense, I never thought of that before, thank you
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    A collider without a Rigidbody is Static (non-moving). This is all it means.

    You don't need a Rigidbody for collisions to happen at all. The actual reason is that Static do not contact other Static because why should they; they should never move. This is why it's often stated, simplistically, that you need a Rigidbody. Adding a Rigidbody makes it Dynamic. Dynamic contact Static. If your projectile were Static and your player was Dynamic then it would contact it because the player would contact the static projectile.

    It's all about the three body types:
    1. Dynamic
    2. Kinematic
    3. Static
    Here's the overview in the manual which details this: https://docs.unity3d.com/Manual/CollidersOverview.html