UNITY – JS Script – OnTrigger – Destroy

UNITY – JS Script – OnTrigger – Destroy

It will destroy the object if it enters or exit the trigger

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

It will destroy the object if it enters the trigger


#pragma strict

function Start () {

}

// Destroy everything that enters the trigger START

	function OnTriggerEnter (other : Collider) {
		Destroy(other.gameObject);
	}

// Destroy everything that enters the trigger END


function Update () {


}

OR

It will destroy the object if it exit the trigger


#pragma strict

function Start () {

}

// Destroy everything that leaves the trigger START

	function OnTriggerExit (other : Collider) {
		Destroy(other.gameObject);
	}

// Destroy everything that leaves the trigger END


function Update () {


}

By |Unity3D|Commenti disabilitati su UNITY – JS Script – OnTrigger – Destroy

UNITY – JS Script – OnTrigger

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()

By |Unity3D|Commenti disabilitati su UNITY – JS Script – OnTrigger

UNITY – JS Script – OnCollision

UNITY – JS Script – OnCollision

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

2. Attach to the Sphere the script


#pragma strict

function Start () {

}

function OnCollisionEnter(collisionInfo : Collision) {
		print("Collision Enter");
}

function OnCollisionStay(collisionInfo : Collision) {
		print("Collision Stay");
}

function OnCollisionExit(collisionInfo : Collision) {
		print("Collision Exit");
}

function Update () {


}

Unity has 3 OnCollision events:

1. OnCollisionEnter

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

2. OnCollisionStay

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

3. OnCollisionExit

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()

By |Unity3D|Commenti disabilitati su UNITY – JS Script – OnCollision

UNITY – JS Script – OnCollision – Destroy

UNITY – JS Script – OnCollision – Destroy

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

CASE 1: OnCollision -> Sphere will be destroyed

Attach to the Cube the script:

NOTICE: if you attach the script to the Sphere, it will not work! Because Sphere can not self collide!

#pragma strict

function OnCollisionEnter (col : Collision)
{
    if(col.gameObject.name == "Sphere")
    {
        Destroy(col.gameObject);
    }
}

Statement: gameObject.name == “Sphere” -> Use the ‘Hierarchy’ Name

CASE 2: OnCollision -> Cube will be destroyed

Attach to the Sphere the script:

NOTICE: if you attach the script to the Cube, it will not work!

#pragma strict

function OnCollisionEnter (col : Collision)
{
    if(col.gameObject.name == "Cube")
    {
        Destroy(col.gameObject);
    }
}

Statement: gameObject.name == “Cube” -> Use the ‘Hierarchy’ Name

CASE 3A: OnCollision -> Multiple Cube will be destroyed

1. Create a Sphere that falls and bource over Multiple Cubes (You have to use Physics 3D engine)
NOTICE: Every Cube can have different names

Attach to every Cubes the script:

#pragma strict

function OnCollisionEnter (col : Collision)
{
    if(col.gameObject.name == "Sphere")
    {
        // Destroy himself
        Destroy(gameObject);
    }
}

CASE 3B: OnCollision -> Multiple Cube will be destroyed

1. Create a Sphere that falls and bource over Multiple Cubes (You have to use Physics 3D engine)
NOTICE: Every Cube MUST have the same name (Cube)

Attach to the Sphere the script:

#pragma strict

function OnCollisionEnter (col : Collision)
{
    if(col.gameObject.name == "Cube")
    {
        // Destroy Cube
        Destroy(col.gameObject);
    }
}
By |Unity3D|Commenti disabilitati su UNITY – JS Script – OnCollision – Destroy

UNITY – JS Script – OnClick – AddTorque

UNITY – JS Script – OnClick – AddTorque

AddTorque OnClick (aggiungere un momento torcente)

1. Create a Box with ‘Collider’ and ‘Rigid Body’

2. Inspector> Rigidbody> uncheck ‘Use Gravity’

3. Attach to the Box the script to AddTorque

#pragma strict
 
function Start ()
{
    
}
 
function OnMouseDown ()
{
    // Debug Message - remove this line if you want
    Debug.Log('Activaction of OnMouseDown!');
    // Add Torque - Momento Torcente
    rigidbody.AddTorque(transform.up * 10);
}

Torque power is addictive, you can try to click over the box more times.

Additional Parameters:

#pragma strict
 
function Start ()
{
    
}
 
function OnMouseDown ()
{
    // Debug Message - remove this line if you want
    Debug.Log('Activaction of OnMouseDown!');
    // Add Torque - Momento Torcente
    rigidbody.AddTorque(-transform.up * 1, ForceMode.Impulse);
}

Torque Power: * 10 less power – 100 more power
NOTICE: Inspector> Rigidbody> Angular Drag, è la resistenza alla forza torcente, maggiore è Angular Drag, minore è la reazione alla forza torcente.

… (transform.up … : local Axis – Y – clockwise (orario)

… (-transform.up …: local Axis – Y – anti clockwise (anti orario)

Additinal Parameters:

rigidbody.AddTorque(-transform.up * 1, ForceMode.Impulse);

– ForceMode.Acceleration: la trasformazione influenza l’accellerazione
– ForceMode.Impulse: la trasformazione influenza l’impulso iniziale
– ForceMode.VelocityChange: la trasformazione influenza il cambio di velocità

By |Unity3D|Commenti disabilitati su UNITY – JS Script – OnClick – AddTorque