- Home /
RigidBody Collision Detection
Hi,
I noticed a new feature with the Rigid Body component, 'Collision Detection'. I have it set to Continuous and it is unstable!
The code that I have is defined in my PlayerMoveScript C# file:
void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "GameObject_1") { ON_TRIGGER = true; } else if (other.gameObject.tag == "GameObject_2") { ON_TRIGGER = true; } //.... }
void OnTriggerExit(Collider other) { if (other.gameObject.tag == "GameObject_1") { ON_TRIGGER = false; } else if (other.gameObject.tag == "GameObject_2") { ON_TRIGGER = false; } }
so I referece the ON_TRIGGER bool type in another script:
void Update()
{
Debug.Log(PlayerMoveScript.ON_TRIGGER);
}
I get true, false, true, false, true, false when I am ON the game object and when the RigidBody->Collision Detection feature is set to Continuous, Discrete or Continuous Dynamic.
Is this a performance issue? It worked fine in 2.6.1 (without this additional feature)
The player has these components attached:
MeshRenderer (which really doesn't have anything to do with CD) RigidBody (with Collision detection to Discrete) Capsule Collider
The game object has these components attached:
MeshCollider MeshRenderer RigidBody (with CD to discrete)
is there a mismatch or something? The player has a child object "bodyMesh" with the same components.
continuous and dynamic collision detection only works for sphere, box or capsule colliders. Also the object gets push away from the collision.
The player is colliding with a $$anonymous$$eshCollider. The player is envelopped within a Box Collider. $$anonymous$$y guess is that their are a number of things that could be wrong, so let me post everything that is attached to the relevent game objects . . .
You DON'T want to use continuous collision detection unless you are using very fast moving objects (fast enough to go from 1 side of the collider to the other within 1 frame i.e. passes through). The performance hit is rather substantial for continuous collision detection.
How can I get my boolean varaible to stay true? It worked fine with 2.6.1
Answer by Proclyon · Nov 10, 2010 at 09:31 AM
When you use a rigidbody u need to consider to use FixedUpdate() rather than Update(), behaviour like the one you described can occur due to slight difference in the timing for the PhysX engine components.
It's all a very precisely tuned clock and when checking for collisions using Continous collision detection you are asking the machine to check a lot more, specifically usefull for checking FAST moving objects, but it costs a lot of effort, having to tune all the time would make for a lot of extra work so Unity3D assumes you want best performance with collision detection unless you need a more specific check for precise calculation (Which in my opinion is a wise decisions).
Unless the objects are going very fast don't use Continuous or ContinuousDynamic collision detection systems. And when using a rigidbody, use FixedUpdate wherever possible, even though I have to admit I am not always doing so myself, I'm not fully confident I understand what is going on when I do use it and I do not like that at all.
Also see the user manual references for more on this
FixedUpdate should be used instead of Update when dealing with Rigidbody.
From: Here_FixedUpdate
and:
Continuous Used for objects which the Continuous Dynamic detection needs to collide with.(This has a big impact on physics performance, leave it set to Discrete, if you don't have issues with collisions of fast objects).
Continuous Dynamic Used for fast moving objects.
From: Here_Continous
Hope it helps :)
I'm having some serious problems understanding just what is going on under the hood with all the collisions and who's calling what. The Unity3D actors are making it tricky for me. Used to doing all the work myself now it's gotta match the engine and components build that exists so it's quite easy to get lost in the maze.
Just keep backtracking and asking where it went wrong if you can't get out!
The problem, was that I had a $$anonymous$$eshCollider attached to my game object. It needed to be a Box Collider.