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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

[Solved] Use OnCollisionEnter or OnTriggerEnter without Rigidbody ?

Discussion in 'Getting Started' started by skyLark9, Jul 10, 2018.

  1. skyLark9

    skyLark9

    Joined:
    May 2, 2018
    Posts:
    135
    It possible to use OnCollisionEnter or OnTriggerEnter in object without rigidbody ?
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,654
    I found these answers:
    https://answers.unity.com/questions/33120/oncollisionenter-doesnt-work-without-rigidbody.html

    https://answers.unity.com/questions/536674/how-to-detech-collision-without-rigidbody.html

    When using these I think both need at least one of the gameobjects to have a rigidbody for collision processing. It doesn't have to be an non-kinematic rigidbody though - so if you have objects you don't wanna do physics (like gravity) for, you can attach a rigidbody still to it, and set the kinematic checkbox. Then when that object hits your trigger or another object with OnCollisionEnter then it should work.
     
  3. skyLark9

    skyLark9

    Joined:
    May 2, 2018
    Posts:
    135
    Thank you MD.
     
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,654
    No problem, good luck! :)
     
  5. haitham-hub

    haitham-hub

    Joined:
    Sep 4, 2020
    Posts:
    1
    guys i have tried it with both my objects with rigid body and one is a bullet but it doesnt work it just pushes my character away and doesnt do anything that i have programed plz help
     
  6. Autarkis

    Autarkis

    Joined:
    Oct 10, 2011
    Posts:
    312
    Can't really help you out without knowing what your code looks like :p
     
  7. Yspadin

    Yspadin

    Joined:
    May 15, 2020
    Posts:
    2
    Did you check the box to Is Trigger ?
     
  8. emptymind01

    emptymind01

    Joined:
    Dec 3, 2019
    Posts:
    2
    Also, it is enough to have a parent with rigidbody. So if you have multiple objects and dont want too many rigidbody than just make a parent and attach rigidbody.
     
  9. Abubakar5415545

    Abubakar5415545

    Joined:
    Aug 27, 2021
    Posts:
    1
    These functions arent called if both of your interacting objects do not have a rigidbody. Atleast one of those have to have a rigidbody in order for OntriggerEnter or OnCollisionEnter to be called.
    Now you have two options. You can add a rigidbody to any one of your objects and keep it kinematic to avoid any hinderances with your scripted procedures. But because that is too costly if you have many objects so instead of going for a whole grid you can use this function

    bool PointInBoxCollider(Vector3 point, BoxCollider2D box)
    {
    Vector2 center = box.bounds.center;
    Vector2 extents = box.bounds.extents;

    Vector2 rightTop = center + extents;
    Vector2 leftTop = center + new Vector2(-extents.x, extents.y);
    Vector2 rightBottom = center + new Vector2(extents.x, -extents.y);
    Vector2 leftBottom = center - extents;
    if (point.x < rightTop.x && point.x > leftTop.x && point.y < rightTop.y && point.y > rightBottom.y)
    return true;
    return false;
    }

    What it does is that it takes a point and checks if that point lies in a given box collider. You can call this function in one of your objects and check in Update if your other object lies within the box collider by passing its transform.position in the function