- Home /
triggers and going through objects
So I have an object moving down towards the ground object. I have a code that says when the object hits the ground, it triggers the ground tag and stops(OnTriggerEnter). Problem is the falling object sees the ground trigger a little late and by the time it stops it is half way into the ground. Is there a way to prevent this from happening or making it more accurate? Any help is greatly appreciated
Answer by Adam Rademacher · Nov 21, 2010 at 05:35 AM
You can do a sphere/capsule cast to check for the trigger to stop its movement early, or you can try non-trigger colliders with OnCollisionEnter.
thanks for the reply. OnCollisionEnter has the same effect; the object is half way through the ground before it's triggered. As for sphere/capsule, I've never heard of it.. can you explain what you're talking about"? thanks
You can think of spheredcasting as the physics system throwing a sphere in the direction (like a ray) and checking to see if it collides with anything. http://unity3d.com/support/documentation/ScriptReference/Physics.SphereCast.html has the scripting reference...you'd basically want to do this and keep the distance very small and the layermask only set to collide with the terrain. Then stop your object from moving when it gets within the distance specified.
Answer by The_r0nin · Nov 21, 2010 at 02:53 PM
You could also check the depth of the penetration (using Contacts and ContactPoints), get the normal of the collision and either move the object back above the ground or apply a force (which would give you a slight bounce effect).
You wouldn't get contacts from a trigger collision, you only receive the collider itself
So only OnCollisionEnter() provides contacts? I thought OnTriggerEnter() used the same colliderInfo? If not, you could change over from a trigger to a collider (like $$anonymous$$said)...