I’m just trying out the features of Unity, building up a simple demo with each feature. I’ve got 6 spheres rotating around a box which occasionally overlap.
I want to detect when the spheres collide. I’ve added a sphere collider with a radius bigger than the spheres.
I’ve written a simple script:
#pragma strict
var bang:AudioClip;
function OnCollisionEnter(collision:Collision){
//for(var contact: ContactPoint in collision.contacts){
// Debug.DrawRay(contact.point,contact.normal, Color.white);
//}
print("wibble");
//if(collision.relativeVelocity.magnitude>1){
audio.Play();
//}
}
Which I’ve saved to a script file in the script folder and attached to each sphere object. An audio clip is attached to the bang property for each sphere object.
With no rigidbody attached to each sphere and isTrigger = false:
nothing happens
With no rigidbody attached to each sphere and isTrigger = true:
nothing happens
With rigidbody attached to each sphere and isTrigger = false:
the spheres bounce off each other
I don’t want the physics bit - I just want to detect and act when the spheres collide - can anyone help please? I also tried it with onTriggerEnter and nothing happened then either.
The rules for collisions and triggers are at the bottom of this page here. From your description, you want to use Rigidbodies with isKinematic set to true.
As you can see from the page that is linked in the answer above, or just click here, then the spheres you have are what is referred to as static colliders. And static colliders cant “see” each other, so what you need to do is add either a Rigidbody Collider (physics) or a Kinematic Rigidbody Collider (non physics one). This basically means to just add a rigidbody and tick the isKinematic on it.
What is usually done with unity is that the player character in your game is a Rigidbody Collider, and every time you need some event to happen you use a collider with “IsTrigger” set to true. Then you can attach scripts that use the OnTriggerEnter/Exit methods to the gameobjects that has the colliders on them.
Which in simple terms means that rigidbodies trigger stuff.
Well that is what unity was built with the physics. Im sorry but it might have to end up like that you can check on youtube to find the answer but the physics were built in and you might possibly cant change the physics. Hope this was useful.