UNITY – JS Script – OnTrigger

1. Create a Sphere that falls and bource over a Box (You have to use Physics 3D engine)

2. Select the Box> Inspector> Box Collider> check ‘Is Trigger’, now the Box does not create collision, instead the Ball pass through it and this can be detected via code.

3. Attach to the Box the script


#pragma strict

function Start () {

}

function OnTriggerEnter (other : Collider) {
		print("Trigger Enter");
}

function OnTriggerStay (other : Collider) {
		print("Trigger Stay");
}

function OnTriggerExit (other : Collider) {
		print("Trigger Exit");
}

function Update () {


}

Unity has 3 OnCollision events:

1. OnTriggerEnter

TRUE at first frame of the collision
VERO al primo frame della collisione

2. OnTriggerStay

TRUE for some frames, until the colliders are still in contact
VERO per diversi frames, finchè i colliders sono in contatto

3. OnTriggerExit

TRUE the first frame when the colliders are no longer in contact
VERO al primo frame che perdono il contatto

NOTICE: put – function OnCollision – OUTSIDE Start() or Update()