- Home /
OnTriggerEnter help
I have a Water Layer and I want to play a splash prefab when an sphere touches it. This code works in some way, it plays the splash when the sphere touches the ground.
function OnCollisionEnter(collision : Collision)
{
if (collision.relativeVelocity.magnitude > 1){
Instantiate(prefab, transform.position, Quaternion.identity);
}
}
This code doesn't work:
function OnTriggerEnter(other : Collider) {
Instantiate(prefab, transform.position, Quaternion.identity);
}
The water have isTrigger on, is a rigidbody with kinematic on and the object is a rigidbody. But the code does nothing, the sphere goes underwater.
Please help me with a solution.
Is this code attached to the sphere or the water? If it is attached to the water, then you should use other.transform.position rather than transform.position.
Also, if the water and sphere are on separate layers, then you should check your Physics settings under Edit>Project Settings>Physics. $$anonymous$$ake sure that the collision table there allows the two layers to collide.
I think the table is allright. The code is applied to the sphere. It will be easy for me if i will keep the code attached to the sphere, I want to implement more functions, but I can't work out this detection.
The water has a mesh collider. I added a box collider and it works but I want to work with a mesh collider because it has waves. It jumps weird in some situations.
Answer by valim · Aug 17, 2013 at 10:15 PM
I'm an idiot, the mesh collider doesn't have a mesh attached to it.
:P well set this as the answer by clicking the checkmark next to the thumb up/thumb down
Answer by IgorAherne · Aug 17, 2013 at 08:39 PM
did you assign a prefab to the "prefab" value in the inspector/ before in the code?
All settings are well. After adding a box collider with isTrigger it worked. The problem seems to be the mesh collider, also with isTrigger on.