Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Collision checking without the need of a rigidbody

Discussion in 'Wish List' started by manuelflara, Feb 15, 2007.

  1. manuelflara

    manuelflara

    Joined:
    Jan 21, 2007
    Posts:
    87
    One of the things I find most frustrating in Unity is that you can't just get OnCollisionEnter events without having one of the affected entities be a rigidbody. Is that because you're using Ageia's PhysiX and is a restriction of that library or is there any other reason?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Well, by definition a collision involves a rigidbody because it's a physics event. If you don't care about physics, you can use triggers which don't need rigidbodies.

    --Eric
     
  3. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
    Theorectically, you can write your own collision function comparing the x, y, z coords.
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    I am interested in hearing what exactly your use case is.
     
  5. manuelflara

    manuelflara

    Joined:
    Jan 21, 2007
    Posts:
    87
    Well, I think OnCollision* events don't get called when using the CharacterController, since it doesn't use physics AFAIK, so having those events called in that case would be helpful (to know when a bullet impacts the player, for example).

    @Eric5h5: do you say triggers work just fine in that situation? That would help.
     
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Actually a rigidbody of a bullet that hits a character controller will still generate a collisionenter event.
     
  7. manuelflara

    manuelflara

    Joined:
    Jan 21, 2007
    Posts:
    87
    Yes, but if that bullet (or whatever it is) isn't a rigidbody, then there's no collision.
     
  8. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    What exactly is your use case of that?
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I think so, though I'm not entirely clear on what the "situation" is. Some context would help you get a better answer. :)

    --Eric
     
  10. manuelflara

    manuelflara

    Joined:
    Jan 21, 2007
    Posts:
    87
    The "situation" is, without any rigidbody at play:

    - Does a Collider collide with a Trigger?
    - Does a CharacterCollider collide with a Trigger?
    - I'd like a CharacterCollider collide with a Collider (say that three times and you'll be my hero).
     
  11. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
    Ideally, if you could disable physics, but keep collision (like simple bare basic collision you would find in any 3D game made from scratch) that would be perfect. I once had a problem like this and was never able to continue because I updated to 10.4.8 >.< lol...

    Hope you can find a way! If so, can you pm your solution (if it's not posted here)?
     
    kobi666 likes this.
  12. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    No, at least one must have a rigidbody attached. If the rigidbody isKinematic is true then you get the trigger messages. If the isKinematic is false then you get the Collision messages.
    If you mean CharacterController, it triggers the Trigger messages. I could not find a CharacterCollider.
     
  13. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    You can mark the rigidbody kinematic to disable physics:

    Is Kinematic If checked, the object will not be driven by the physics engine, but can only be manipulated by its Transform
     
    shacharoz likes this.
  14. manuelflara

    manuelflara

    Joined:
    Jan 21, 2007
    Posts:
    87
    I just did some tests:
    - CharacterController and Trigger -> Don't collide (OnTriggerEnter is not called)
    - CharacterController with a kinematic rigidbody and Trigger -> Don't collide either

    So I don't know how one may create an object that is collideable by a CharacterController AND I know when they collide...
     
  15. tsphillips

    tsphillips

    Joined:
    Jan 9, 2006
    Posts:
    359
    I have been doing what Keli recommended and it works fine -- add a Rigidbody, but check the isKinematic attribute. I still keep complete control over how the game object moves, but I am able to use trigger events because of the Rigidbody.

    Is there a particular reason why you want to avoid using a Rigidbody?
     
  16. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    Seems to be working for me. Here is a work of high art for you to look at :wink: The standard keys move one of the boxes. On your scene you do need to make sure that the collider is set to be a trigger (already set in this example).
     

    Attached Files:

  17. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    I wrote this joke not to be funny but to illustrate the problem I was having with colliders, triggers, and rigidBodies.
    Now why would you want the overhead of rigidBody physics if all you want is a *nice* intersection test?

    I'm working with ~400 instants of a rigidBody colliding themselves (I want to cull instants to instants colliding) and with another 30 to 50 target objects. I'm looking for other solutions beside rigidBodies triggers. Maybe rayCasting?
     
  18. Zogg

    Zogg

    Joined:
    Mar 28, 2009
    Posts:
    157
    I have a similar problem: I have a player character (snowboarder) who is moved by my code, I don't want the Physics engine to mess with.

    On the other hand, when he touches the fence of the track, I would like to know where he touched it. Hence I would like to use OnCollisionEnter (and not OnTriggerEnter) because the former has information about the collision points whereas the latter only gives me the collider (the fence).

    Unfortunately, in order to be able to use OnCollisionEnter, the Unity Law says I need a non-kinematic rigidbody - and in this case I have problems hindering the Physics engine messing with my snowboarder... I tried to trick him into behaving like a kinematic rigidbody (reducing mass, setting drag to insane values etc.) but so far without success.

    The only solution I found so far is to forget OnCollisionEnter and to launch "manually" rays toward the fence to test how near and in which direction it is.

    Why does this stupid "non-kinematic rigidbodies only" law exist anyway ?
     
    imwundev likes this.
  19. Ookoohko

    Ookoohko

    Joined:
    Sep 29, 2009
    Posts:
    103
    Hi,

    I've been facing the exact same problem - I'd need to get collision between colliders and kinematic rigidbodies.

    The problem is that my main character has a kinematic rigidbody, so it does not collide with any colliders (only triggers) in the world... Then, I have some non-kinematic rigidbody objects (using physics engine) which do NOT collide with triggers. So, I can either collide my character or everything else, but not both :-(

    I'd really like to see Unity dev team add support for kinematic rigidbody vs. colliders collision (using f.ex. optional per-rigidbody flag to enable this or so?)

    br,
    Sami
     
  20. Ted-Chirvasiu

    Ted-Chirvasiu

    Joined:
    Sep 7, 2010
    Posts:
    381
    After 4 years is still no solution for this problem? :(
     
  21. Writer's Block

    Writer's Block

    Joined:
    Jan 21, 2012
    Posts:
    4
    I would be intrested in this too.
     
  22. td-lambda

    td-lambda

    Joined:
    Jul 14, 2011
    Posts:
    14
    Guys, the answer was posted already:

    This basically means that you're not creating unnecessary physics overhead as long as the rigidbodies are kinematic.
     
  23. AshMcConnell

    AshMcConnell

    Joined:
    Aug 10, 2011
    Posts:
    11
    Sorry, but that's not true. You only get trigger collision events if you check Kinematic, which does not give you enough information in some situations.

    I think it should be an option to get OnCollisionEnter(Collision collision) calls so that the collision response can be manually handled by the user and not handled by Unity (PhysX).

    I was hoping to do this with my old C++ car physics engine for http://onlineracingchampionship.com , but it doesn't seem possible.
     
  24. SinisterMephisto

    SinisterMephisto

    Joined:
    Feb 18, 2011
    Posts:
    179
    I just found out about this an i am really pissed. Dude to unities physics iterator no having any callback functions i had to write my own projectile physics and wanted to leverage unity's collider system but no it wont work unless i make it a rigidbody. This insane.
    Kinematic rigid bodies dont work either. Ground collision doesnt trigger if there is no gravity either.

    Really frustrating
     
  25. actuallystarky

    actuallystarky

    Joined:
    Jul 12, 2010
    Posts:
    188
    This problem has proven to be an absolutely massive timesink on my project and judging by recent new features, Unity Technologies seem to be completely oblivious to its implications.

    This has been my progress with this issue.

    1) I need a player character. Looks like Unity has thoughtfully provided a Character Controller. Excellent. I'll use that.

    2) I need something chasing the player character that is also a character. I guess I'll use a Character Controller for that too.

    3) ok. Now I need to detect when they're colliding. Mmmm. Doesn't seem to work. <quick search of manual and forums> Looks like I need to give colliders to my character controllers. That's a little weird as now I have two sets of collision but okay.

    4) Collision mostly works but is a little flakey. I see - OnControllerColliderHit only registers when it would collide with something during it's move. So if something bumps into it, no collision is detected. Okay, I'll have to check on both parties for collisions. Well, that's kinda' annoying but okay.

    5) Now I get warnings telling me that I should attach rigidbodies to my characters for performance reasons. After checking the profiler that sounds like a good idea. I'll just add the rigidbodies and mark them isKinematic = true.

    6) My performance is much better but now my collisions aren't working - again. <more searching through forums and documentation> Looks like the system doesn't register collisions between two kinematic objects. Great. <Pull out much hair>

    7) Implement horrid hack which mostly works but introduces strange random behaviour - set isKinematic to false but switch on physics constraints in all 3 axis.

    8) Determine that the problem isn't the physics system, it's the Unity character controller stepping outside of it. A physics-driven character controller would not have any of these issues.

    9) Swear never, ever, EVER to use the character controller again.

    10) Scream to the heavens when you find that UT have implemented path finding as a custom character controller that has all of the above issues with collision and this time there's no way around it.
     
  26. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    The first thing I did when I learned that U3D had a 'character controller' was to ignore it. There's no way I'm building a game with such fundamental functionality Black-boxed.

    That said, this *problem* has apparently been around for 4 years, yet I've heard surprisingly little about it in my time on the forums. To me that says that there are perfectly good ways of implementing the needed functionality - you might have to rethink the problem.

    Just to through it out there - I prefer to use a rigid body with correct usage of applying forces and some smart use of clamping when building a characters movement.

    So yeah, hopefully you have a solution and have learnt a tad about Unities strengths and weaknesses.

    Worse comes to worse... you do *not* have to use U3D's physics. I have in the past ported an entire 3D physics engine in unity - not only does it give you more control [both callbacks and the ability to modify source] it is nicely multi-threaded :)

    If you need some help on this, or other problems, feel free to add me to skype - as a fellow Queenslander I'd be glad to help!
     
  27. actuallystarky

    actuallystarky

    Joined:
    Jul 12, 2010
    Posts:
    188
    You may regret offering that. :)

    At the moment I'm just finishing up my first indie project and it's going to ship with the nest of hacks as is. It works. And that will do for now.

    Moving forward the solution is obvious - simply roll your own character controller. No problem there.

    The thing that has me really upset is how UT chose to implement their pathfinding - with another custom outside-of-physics controller. So now what happens in the following hypothetical situation?

    I have some AI controlled with the awesome new pathfinding controller.
    I have a player who is also controlled with the awesome new pathfinding controller because my game uses a click-to-move control system.
    How do I detect collisions between the AI and the player?
    How do I have the AI or the player react to physics forces or collisions in the world?

    I can either ditch the UT pathfinding and use another one or I can ditch the UT physics / collision system and use another one.

    Call me crazy but I would have thought that Unity's internal systems would have been designed to be used with each other. I am more than happy to be shown how I am completely wrong and have missed something obvious and easy.
     
    entropicjoey1, ctaggart and twda like this.
  28. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Meh, worse comes to worse I'll just block ya :p

    I know little to nothing about pathfinding or U3D's implementation, so seek advice elsewhere :p

    However I do note that unity3d do expose the CalulatePath function among others - so while you might still have to do some work [e.g. collision avoidance] there still some valuable functionality available.
     
  29. actuallystarky

    actuallystarky

    Joined:
    Jul 12, 2010
    Posts:
    188
    Yes, there is some exposed path-finding functionality so it seems possible to implement some kind of path-finding rigid body. The documentation is sparse and the only people I've heard of trying to use it this way report issues but it's probably doable. However, there's no way of getting the avoidance behaviour without using the custom controller.

    It's not a show-stopper, but the implementation could be much better.
     
  30. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    From the sounds of it is the same as the character controller - i.e. hidden code == useless code.

    :(
     
  31. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,816
    You can use Bound.Intersects on a mesh to detect if it's hit another collider.

    To get around the problem of having a character controller not causing a trigger to fire, simply create a child gameobject with another collider attached, then propogate the detection up into your character script.
     
  32. actuallystarky

    actuallystarky

    Joined:
    Jul 12, 2010
    Posts:
    188
    And for performance reasons that child collider, being dynamic, should have a rigidbody attached (or cost you way more CPU than it should). That rigidbody will need to be marked kinematic aaaaaaaaand we're back to square one.
     
  33. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    763
    If I understand this correctly, I can't have an object that gets notified when it collides with something, yet doesn't actually collide with it, other than with a trigger. So if I have a camera that I want to be told when it goes through a wall so I can use my own logic to work out where it should be instead, I can't really do it.
     
    robotintervention and j_zeitler like this.