unity3d

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

UNITY – JS Script – OnClick – Gravity – AddForce

UNITY – JS Script – OnClick – Gravity – AddForce

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

2. Inspector> Rigidbody> uncheck ‘Use Gravity’

3. Attach to the Box the script to Add Gravity

#pragma strict
 
function Start ()
{
    
}
 
 
function OnMouseDown ()
{
    // Debug Message - remove this line if you want
    Debug.Log('Activaction of OnMouseDown!');
    // Activate gravity
    rigidbody.useGravity = true;
}

OR

OnClick Add Force + Add Gravity

#pragma strict
 
function Start ()
{
    
}
 
function OnMouseDown ()
{
    // Debug Message - remove this line if you want
    Debug.Log('Activaction of OnMouseDown!');
    // Add Force
    rigidbody.AddForce(-transform.forward * 50);
    // Activate gravity
    rigidbody.useGravity = true;
}

Force Direction: -transform.forward OR transform.forward

Force Power: * 50 less power – 500 more power
NOTICE: Inspector> Rigidbody> Drag, è la resistenza ad una forza, maggiore è Drag, minore è la reazione alla forza

Additinal Parameters:

rigidbody.AddForce(-transform.forward * 50, ForceMode.Acceleration);

– 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 – Gravity – AddForce

UNITY – JS Script – Input.GetAxis – AddTorque

UNITY – JS Script – Input.GetAxis – 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

public var amount : float = 50f;


function FixedUpdate ()
{
    var h : float = Input.GetAxis("Horizontal") * amount * Time.deltaTime;
    var v : float = Input.GetAxis("Vertical") * amount * Time.deltaTime;
    
    rigidbody.AddTorque(transform.up * h);
    rigidbody.AddTorque(transform.right * v);
}

The Input.GetAxis Torque is additive!
You can try click arrow keys more times to see the effect!

By |Unity3D|Commenti disabilitati su UNITY – JS Script – Input.GetAxis – AddTorque