I would like to obtain collision information (i.e. contact points, normals) for a collision between two colliders. I was happy to see that this info is already present in the Collision class, which I can access from the OnCollisionEnter() function. However, I need this information for “Trigger-like” objects that should not literally be colliding and bouncing off of each other.
The logical thing to do would be to make my objects Triggers and use the OnTriggerEnter() function. However, the OnTriggerFunction does not provide collision information like OnCollisionEnter does. (Not sure why this choice was made for the scripting API - I think it would have made more sense to give these two functions parallel behavior, but never mind.)
So it looks like the only way I can get the collision information I need is to use OnCollisionEnter(), but this only works when at least one of my objects is a NON-kinematic rigid body. So whenever it collides with another object it bounces off and flies away - not the behavior I need.
Each of these “solutions” either makes the objects into Triggers (thereby denying me access to the information I need), making one of the rigidbodies Kinematic (which prevents OnCollisionEnter() from ever firing), or by using Physics.IgnoreCollision() (which also, obviously, prevents a collision from firing).
Am I SOL, or is there a way to get collision information from Trigger-like objects?
Hi! In my project(sword fighting) I solvded this problem that way:
1)Attach a rigidbody to “weapon” gameobject. Set isKinematic = true.
2)Create empty gameobject “collider1” and parent it to “weapon”.
3)Attach primitive collider to “collider1”.
4)Attach rigigidbody to “collider1”. Set all Constraints(FreezePosition and FreezeRotation) = true.
5)Attach FixedJoint to “collider1”. Set ConnectedBody = “weapon”.
6)Repeat (2)-(5) to create and customize compound collider for your “weapon” object.
“weapon” is object that you move by script or animation, multiple childrens collide[n] is needed to use as compound collider, because meshcollider doesn’t give collision with other meshcollider. For example - I used 15 small boxcolliders to make a compound sword collider more realistic.
As a result you can read “collision”, “contact point” and “normal” from OnCollisionEnter, but Physics doesn’t move your colliders/objects.
If you use Character motor, set “Use Fixed Update” = false to avoid “weapon” rigidbody move from hand, during your character walks.
You can set the “drag” and “angular drag” values of rigidbodies to 1.0 (or greater). These settings will slow the object’s movement to zero at a rate based on how high the drag value is (higher drag = faster stopping). With an exceedingly high value, you’ll only have the very faintest bit of a nudge when the two objects collide.
You can set drag in-game, so you could set drag to zero when the thing should move, then “really high” when you need it to stay in place.
If that isn’t what you’re looking for, what specific effect are you looking to get (billiards or something)? We might be able to give a workaround method for you.
I’ll describe my particular situation below. However, even if there are better ways to get at the particular effect I’m trying to achieve, it seems to me that accessing collision information for Trigger-like meshes is an extremely useful thing to have anyway. The long list of previous posts is good evidence of this need: essentially a desire to do Intersection tests rather than Collision tests.
I’m playing around with lights and shadows, and want to have a shadow that “carves away” part of a spotlight (imagine a solar eclipse where the moon carves out a circular arc of the sun).
If this were purely a visual effect, I wouldn’t be anywhere near collision detection and would probably just use a shader effect (or the stencil buffer, if I had access to it )
Instead (for good reason) I need the shadows and light themselves to be meshes. I am using the shadow volume technique to calculate a real-time shadow volume mesh, and I am planning to use a similar technique to calculate a “light volume” for the spotlight. But when the light and shadow volumes overlap, I’d like to calculate a new shadow volume mesh that is essentially “New Shadow Volume = Old Shadow Volume - Light Volume”
To do this I was trying to access contact points so I could do some procedural mesh “carving” (add the contact points to the shadow volume, then delete the points that were in the light volume).
I hope my explanation is somewhat clear - let me know if I need to be more specific!
OnEnterTrigger doesnt have all the extra bells and whistles on purpose. When dealing with so many collisions that it is slows the game, you use triggers. They have other uses of course but this is why it was stripped down.
Personally, I would try this:
Save the velocity and angular velocity
On “OnEnterCollision” read all the info you need
Restore the velocity and angular velocity
You are going to have to try a few things to figure out exactly when/where to save and restore the values but I would start with fixed update, if it doesnt work read this: Unity - Manual: Order of execution for event functions