1
votes

I am working on a tower defense style game. I have a player gameobject with a sphere collider, and turrets firing projectiles at it. The projectiles have capsule colliders. When the player game object is moving, the OnTriggerEnter events fire for the colliders of the projectile. When the player is not moving, they do not. The same goes for when I add new turrets. The turrets have a large sphere collider to determine if a game object is close enough to shoot at. When I add a new turret to a scene while playing, the OnTriggerEnter does not occur until I move the player game object, even though it is already within the sphere collider of the turret.

video: http://screencast.com/t/x1QKtNEK

How can I work around this? I doubt this is a bug, just my lack of understanding the Unity update, or something to do with collider triggers.

1
I had a similar problem. Does your game object have a rigid body attached to it? Or the projectiles? - Varaquilex

1 Answers

2
votes

I don't know if this is a bug, but unity's documentation states that

"OnTriggerStay is called almost all the frames for every Collider other that is touching the trigger."

Maybe same applies OnTriggerEnter and "almost" is the problem here.

Do you have rigidbody on at least other part of the collision? Same documentation (and OnTriggerEnter documentation) also states that

"Note that trigger events are only sent if one of the colliders also has a rigidbody attached".

Hopefully you find a proper solution to the problem, but as a workaround you could try to set player's collider into a child GameObject and move it little on every frame. If you don't have anything rendering related on the child GameObject no one will notice the hack.

Or projectiles can be handled with just calculating the distance to the player in their update function. Turrets can do the same calculation in their start function.