I have been having a great deal of trouble trying to get collisions working properly in my game.
The problem is that no matter what approach i use, they all have the same problem: Collisions will consistently fail to register.
I am currently using the OnTriggerStay method (since for some reason it is detecting more collisions than OnTriggerEnter).
I’ve also tried the OnCollision methods, using every combination of collision detection, and this worked no better. But for my particular game the characters need kinematic rigidbodies.
I thought i had solved the issue when i made the colliders themselves larger. which may have improved things, but it is still not solved. Here is a picture of my colliders.
If i stand IN THE EXACT SAME PLACE. and attack over and over and over. currently i’d say about 95% of collisions are registering (in the past it was as low as 50%). But STILL occasionally they fail to do so.
Can anyone explain to me how it is possible for this not to register a collision???
Obviously the basics are set up correctly since 95% of collisions are working.
my only thought is that the attack animation is too fast, but it is no faster than attacks in any other game. Is it possible that some particular animations are not suited to what i’m trying to do or is there another reason?
I guess that’s possible, especially if the collision detection mode is “Discrete”. However, it needs to be really fast in order to miss that big box.
1) First, you need to know if collision between colliderA and colliderB is “compatible”. Check the next page (scroll down to the bottom): https://docs.unity3d.com/Manual/CollidersOverview.html
I mention this because it is possible that the messages you are triggering are coming from some other collider, and not from the one you really want.
2) Assuming 1) is OK, then you need to know when things should move (the order is very important). This is Unity’s main loop: https://docs.unity3d.com/Manual/ExecutionOrder.html
Based on that big loop:
- Collision messages/events (what you are interested in) occur after the physics simulation during the “Physics” phase.
- Animation updates (“internal animation update” blocks) occur in two different phases: “Physics” (50 hz by default, although it can be more than that) and “Game Logic” (with Vsync = your monitor refresh rate).
The reason why you are missing some of those messages is because the sword gets updated during the “Game logic” phase (Animator’s default behaviour). It makes sense to update the animation using the “AnimatePhysics” update mode:
https://docs.unity3d.com/ScriptReference/AnimatorUpdateMode.html
“AnimatePhysics: Updates the animator during the physic loop in order to have the animation system synchronized with the physics engine.”
I guess that should be enough. Finally, since you are not updating the animation during the Game logic phase anymore, you should consider enabling interpolation on the sword rigidbody*.*