- Home /
Prefab instantiated does not fire trigger
I have a bomberman-like game. To realize dying for the player I Spawn GameObjects with a particle effect and a box collider trigger. When the Player OnTriggerEnter/Stay is called, the player dies.
The strange thing about is the following: The Trigger is not called when the explosion is spawned inside my player via script! But When I move the player a little bit, everthing works! Neither OnTriggerEnter nor OnTriggerStay is called.
But when I place my object via the editor the same way inside the player, everthing works as expected.
Everthing is set up as it should (hopefully): Player has rigidbody with capsule collider.
Here is my code to spawn the explosion:
private void SpawnExplosionPrefab(Point position, bool startPlayAudio)
{
var worldPosition = GameController.Instance.GetWorldPosition(position);
var explosion = (Transform)Instantiate(ExplosionPrefab);
if (startPlayAudio) explosion.audio.Play();
worldPosition.y = explosion.position.y;
explosion.position = worldPosition;
}
Answer by Oribow · Jan 03, 2015 at 08:33 PM
Try to avoid using a trigger. Instead use the Physics.OverlapSphere() to check if the player is in explosion range.
Collider[] col Physics.OverlapSphere (worldPosition,explosionRange);
foreach(Collider c in col)
{
if(c.tag=="Player")
{
//kill the player
}
}
OverlapSphere does not suit my requirements properly I think, but this will be a small workaround.
Does Anyone has a logical answer why this does not work as expected?
OnTriggerEnter does not work when bomb instantiates inside player because it never hits player's collider.
But when u move your player or place the object inside the editor. It hits its collider and thus works.
I think the collision intersection is also provided by spawning it via script the same way as I place it in the editor. I testet it stopping the scene and checking the colliders. I will use a workaround anyway as this does not seem to be viewed as a bug.