- Home /
Having trouble getting triggers to work with two moving cubes
The script I have here is attached to the falling cube, and is using a Sphere collider with trigger unmarked. It is attached to a prefab that is instantiated above the screen and dropped into the scene. The idea is that once it reaches it's intended Y value it will enable the boolean "canKill" and will destroy any trigger items it is touching. I have not been able to get the triggers to recognize an OnTriggerEnter or OnTriggerStay. What am I doing wrong?
Below is the script attached to the falling cube.
private var yVal : int; var speedToDrop : float = 10; static var canKill : boolean = false; private var inBlastRadius : boolean = false;
function Awake () { yVal = crossHair.dynamiteY; }
function Update () { var thisVector3 : Vector3 = this.transform.position; if(thisVector3.y <= yVal) { canKill = true; Destroy(this.gameObject); }
this.transform.Translate(0, 0, speedToDrop * Time.deltaTime);
}
function OnTriggerEnter(hit : Collider) { Debug.Log("Entered"); inBlastRadius = true; if(canKill && inBlastRadius) { Destroy(gameObject); } }
function OnTriggerExit(hit : Collider) { inBlastRadius = false; }
function OnTriggerStay(hit : Collider) { Debug.Log("In trigger"); inBlastRadius = true; if(canKill && inBlastRadius) { Destroy(gameObject); } }
The other cube is moving horizontally across the screen with a sphere collider marked Trigger.
Answer by David Gutierrez · Sep 03, 2010 at 12:40 AM
http://unity3d.com/support/documentation/Manual/Physics.html
You didn't mention if the Prefab had a rigidbody or trigger attached. But the above link has a table towards the bottom that shows with what other objects a trigger will actually go off.
The prefab is simply a Sphere collider. It is not marked as trigger.
Add a rigibody to either the cube or the sphere, mark it as kinematic, and unaffected by gravity and it should work.
Aha! I wish I'd looked a little closer at the docs before posting. Thanks for pointing that out.
Your answer
Follow this Question
Related Questions
OnTriggerEnter2D not working, alternatives? 2 Answers
issues with on trigger enter and exit 1 Answer
Navmesh agent problem 1 Answer
OnTriggerEnter 1 Answer
Spawn Random Item On Trigger Enter 2 Answers