- Home /
OnTriggerEnter() Works only Sometimes, way to make it work all the time?
The image shows a sword & a wall collider on the column. The sword has a collider with "isTrigger" checked. The column is a box collider.
Often when the sword swings and makes contact with the collider, it registers. However, about 40% of the time or so, it does not and the sword goes through the box collider without any registration of a collision. This happens with other objects to. (Right now it's just walls and static objects, but soon it'll be enemies, and so I'd hope to make sure that all collisions are registered.) The code -- a script attached to the sword -- is below -- any help would be greatly appreciated!!
function OnTriggerEnter(other : Collider)
{
print("Weapon Hit: " + other.gameObject);
playerObject.GetComponent(joystickPlayerTurn).PushBack(10.0);
Instantiate(wallHitPrefab, transform.position, Quaternion.identity);
}
After a swing, does the sword collider properly exit the 3D space of the object you are trying to hit? If the colliders remain even slightly overlapped the Enter function will not be re-called.
Yep, it goes right back to the start -- it's a quick "swing sword" animation. I've confirmed that it's not hitting the floor or anything else, by limiting to tagged walls, which they all are now. Unfortunately it's still not working properly :/
What is your frame rate like? I'm not 100% sure, but if the sword moves so fast that on frame X it is on the right side of the wall and frame X + 1 it is on the left side, then it will have never entered the wall and will not produce a collision. You could slow the animation down, increase the framerate, etc, to test this theory.
If you need the animation to be really fast still, an alternative would be to create a mesh that covers the sweeped volume of the sword, i.e. a mesh that covers all areas that the sword will enter while swinging. Then, when you swing the sword, perform a collision of the poly collider with the world to see if it hits anything. Since the it is static, it will always show the collision.
Thanks for the idea, but slowing it down to 20% normal speed didn't change anything :/
Hmm...still seems like it's a problem of missing the entry, or maybe the entire bounding polygon moves inside or outside of the wall all at once, looks like your colliding polygon is just at the end of the sword in the image. If you aren't using a bounding box for the sword, try that, and make sure it extends down to the character's hand. Two other things to try are:
Turn on Continuous physics for the sword or Reduce the time in Project->Settings->Fixed Timestep.
Answer by Ziron999 · Sep 01, 2014 at 11:25 AM
I'm not sure if you got this fixed but using a rigidbody helps with this situation. For some reason just having that seems to do a better job with collision with everything...
OnTriggerEnter will never trigger unless at least one of the objects has a Rigidbody.
Answer by taimur_azhar · Jul 19, 2017 at 06:28 AM
I had the same problem, try using these steps.
-Apply colliders and rigidBody on both object, you can check iskinematic of rigidbody. -Make collider side big enough that it doesn't get skipped due to frames (because of collider size is small and and player is in another frame and collider is on another frame, it'll miss the trigger)
Answer by Soniya_unity · Jul 20, 2017 at 09:35 AM
go to Edit -> Projectseting -> Time and change the fixed timestamp value, by defalut it is 0.02 cahnge it to 0.03.
Your answer
Follow this Question
Related Questions
OnTriggerEnter is not running! Need help with collision. 2 Answers
Animation with frozen player ?? 0 Answers
How to play a sound when the player steps on a toy? 1 Answer
Trigger respawn after collision? 3 Answers
Trouble Descaling 2D object 1 Answer