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)

Unity – Scripting – GUITexture

Unity – Scripting – GUITexture

Unity – Scripting – GUITexture (get from gui and code it)

GUI Texture

1. MAIN TOP MENU> GameObject> Create Other> GUI Texture

2. The text will create the GameObject ‘UnityWatermark-small’ in the Viewport

3. Project> Assets, select ‘UnityWatermark-small’> Inspector, setup the texture default parameters

4. Attach this JS Script at GameObject ‘GUI Text’:

#pragma strict
 
        // Assign someTexture to the guiTexture
	var someTexture : Texture2D;
	guiTexture.texture = someTexture;   

5. DRAG AND DROP a Texture from Assets to Inspector> GUITexture> JS Script> Some Texture

Properties List

border The border defines the number of pixels from the edge that are not affected by scale.
color The color of the GUI texture.
pixelInset Pixel inset used for pixel adjustments for size and position.
texture The texture used for drawing.

By |Unity3D|Commenti disabilitati su Unity – Scripting – GUITexture

Unity – Scripting – GUIText

Unity – Scripting – GUIText

Unity – Scripting – GUIText (get from gui and code it)

GUI Text

1. MAIN TOP MENU> GameObject> Create Other> GUIText

2. The text will create the GameObject ‘GUI Text’ in the Viewport

3. Project> Assets, select the text> Inspector, setup the text

4. Attach this JS Script at GameObject ‘GUI Text’:

#pragma strict
 
 guiText.text = "Hello Monkey!";         

5. Play and you will see “Hello Monkey!”

Properties List

alignment The alignment of the text. It is object pivot-based
anchor The anchor of the text.
color The color used to render the text.
font The font used for the text.
fontSize The font size to use (for dynamic fonts).
fontStyle The font style to use (for dynamic fonts).
lineSpacing The line spacing multiplier.
material The Material to use for rendering.
pixelOffset The pixel offset of the text.
richText Enable HTML-style tags for Text Formatting Markup.
tabSize The tab width multiplier.
text The text to display.

By |Unity3D|Commenti disabilitati su Unity – Scripting – GUIText