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

Unity – Scripting – OnGUI – Toggle (Interruttore)

Unity – Scripting – OnGUI – Toggle (Interruttore)

Videogames Development – Unity – Code GUI Objects – Toggle (Interruttore)

Text Toggle

1. Assign the script to the Camera:

#pragma strict

        
	// Text Toggle START
        private var toggleTxt : boolean = false;
	function OnGUI () {
	if (GUI.Toggle(Rect(10, 10, 100, 30), toggleTxt, "A Toggle text")) {
		print ("You clicked the toggle!");
	}
        // Text Toggle END
}

Statement:
GUI.Toggle(Rect(10, 10, 100, 30), toggleTxt, “A Toggle text”
GUI.Toggle(Rect(x, y, x, y), bool, “text content”

Image Toggle – Use images as buttons

1. Assign the script to the Camera:

#pragma strict
        
    // Image Toggle START
	var aTexture : Texture;
	
    private var toggleImg : boolean = false;
    
	function OnGUI () {
	// Assign image from Inspector Controller
	if(!aTexture) {
			Debug.LogError("Please assign a texture in the inspector.");
			return;
		}
	if (GUI.Toggle(Rect(10, 50, 50, 50), toggleImg, aTexture)) {
		print ("You clicked the toggle!");
	}
    // Image Toggle END
}

2. Select the Camera> ‘Inspector’> Script> DRAG AND DROP a texture from ‘Assets’ to Script> ‘ATexture’ variable.

3. Play and enjoy!

By |Unity3D|Commenti disabilitati su Unity – Scripting – OnGUI – Toggle (Interruttore)