- Home /
OnTrigger, OnCollison
Hi,
Im having trouble with OnTrigger and OnCollision, I've built a animated mouse and paired a weapon (a pole) by script to its left hand, the weapon has a capsule collider on it and I've set its trigger to on, now in the scene I have a box (cheese box) with a box collider so when the mouse swings the weapon and hits the box the script attached to it (and below) should be registering the hits but its not working, I've also tried OnCollision with the weapon trigger off again with no luck, is it something to do with the weapon being paired by script to the left hand?
Any help would be greatly appreciated.
var explosionPrefab : Transform;
var sparklePrefab : Transform;
var WoodSound : AudioClip;
var MetalSound : AudioClip;
var BrickSound : AudioClip;
var audioSources;
private var CheeseBoxHitCount = 0;
function OnTriggerEnter (other : Collider)
{
audioSources = new AudioClip[5];
if(other.gameObject.tag == "CheeseBox")
{
audio.PlayOneShot(WoodSound);
CheeseBoxHitCount = CheeseBoxHitCount + 1;
}
else if(other.gameObject.tag == "MetalBox")
{
audio.PlayOneShot(MetalSound);
}
else if(other.gameObject.tag == "Wall")
{
audio.PlayOneShot(BrickSound);
}
var explodePosition : Vector3 = transform.TransformPoint(Vector3.zero);
Instantiate(explosionPrefab, explodePosition, Quaternion.identity);
if(CheeseBoxHitCount > 2 && other.gameObject.tag == "CheeseBox")
{
Destroy (other.gameObject);
var sparklePosition01 : Vector3 = other.gameObject.transform.position;
Instantiate(sparklePrefab, sparklePosition01, Quaternion.identity);
CheeseBoxHitCount = 0;
}
}
Answer by David C · Jun 03, 2012 at 08:26 AM
I may be wrong here but when using OnTrigger one of the two colliders needs to have a rigidbody attached as well as at least on of the colliders need to have isTrigger checked.
Try this and if it doesn't work come back and I will try and help!
Indeed,you need to add a rigidbody and tick IsTrigger on one of them.
OnTrigger is a way to check collision without using the physics engine. When using OnCollision, the engine makes the two rigidbodies bounce against each other depending on many variables. With OnTrigger you simply can check if they are colliding but nothing physical happens.
I just clarify since you say you are not sure.
Ok, I've tried all that with no luck so I've gone right back to basics, I'm using a modified version of the 3RD person controller in the standard assets folder, so I've created a new project and 1 scene in that scene I have the 3RD person controller, a plane (floor) and a box with ridged body, I've attaché the script to the 3RD person controller with a debug line to tell me when it collides and what with (Debug.Log("Collided with " + gameObject.name);)
I've tried this with OnTrigger to with trigger ticked, I've also tried it with the script on the box and I can not get it to detect.
Is it something to do with the way the 3RD person controller is written? I have noticed it already has a collider built in, I've tried adding an other with the same results ..
Your answer
Follow this Question
Related Questions
OnTrigger & OnCollision not working? 2 Answers
Performance difference between OnCollision and OnTrigger messages? 2 Answers
How to change the position of the camera when colliding with a GameObject? 1 Answer
How to get the collided plane of a mesh, not just the object,How to get the collided plane of a mesh 1 Answer