Unity – Look At 3D – Mouse Position

Unity – Look At 3D – Mouse Position

The Code:

        // Performs a mouse look.
	var horizontalSpeed : float = 2.0;
	var verticalSpeed : float = 2.0;
	
	function Update () {
		// Get the mouse delta. This is not in the range -1...1
		var h : float = horizontalSpeed * Input.GetAxis ("Mouse X");
		var v : float = verticalSpeed * Input.GetAxis ("Mouse Y");
		transform.Rotate (v, h, 0);
	}
By |Unity3D|Commenti disabilitati su Unity – Look At 3D – Mouse Position

Unity – Repeiting a function

Unity – Repeiting a function

Invoke Repeat

#pragma strict
 
var x: int;

function Start()
{
    // InvokeRepeiting (function name, delay time in seconds, repeat every x seconds...)
    InvokeRepeating("myFunction", 2, 1);
}

function myFunction()
{
    var x = x++;
    Debug.Log("Invoked " + x);
}

The result is:

Invoked 0
Invoked 1
Invoked 2
Invoked 3
… to infinity and beyond!

Cancel Invoke

        // Starting in 2 seconds.
	// a projectile will be launched every 0.3 seconds
	var projectile : Rigidbody;
	InvokeRepeating("LaunchProjectile", 2, 0.3);

	// Cancels the repeating invoke call, 
	// when the user pressed the ctrl button
	function Update() {
		if (Input.GetButton ("Fire1"))
			CancelInvoke("LaunchProjectile");
	}
	function LaunchProjectile () {
		instance = Instantiate(projectile);
		instance.velocity = Random.insideUnitSphere * 5;
	}
By |Unity3D|Commenti disabilitati su Unity – Repeiting a function

Input Control – Arrow Key – Advanced

Input Control – Arrow Key – Advanced

#pragma strict

//--------------------------------------------------------------------
// USER INPUT CONTROL START
//--------------------------------------------------------------------
public var inputMoveSpeed : float = 10f; // increase -> faster
public var inputTurnSpeed : float = 50f; // increase -> faster

function UserInputControl()
{
    // Arrow key to move object
    if(Input.GetKey(KeyCode.UpArrow))
        transform.Translate(Vector2.up * inputMoveSpeed * Time.deltaTime);
    
    if(Input.GetKey(KeyCode.DownArrow))
        transform.Translate(-1*Vector2.up * inputMoveSpeed * Time.deltaTime);
    
    if(Input.GetKey(KeyCode.RightArrow))
        transform.Translate(Vector2.right * inputMoveSpeed * Time.deltaTime);
        
    if(Input.GetKey(KeyCode.LeftArrow))
        transform.Translate(-1*Vector2.right * inputMoveSpeed * Time.deltaTime);
        
    // Z X keys to rotate object
    if(Input.GetKey(KeyCode.X))
         transform.Rotate(Vector3.back,inputTurnSpeed * Time.deltaTime);
         
    if(Input.GetKey(KeyCode.Z))
         transform.Rotate(-1*Vector3.back,inputTurnSpeed * Time.deltaTime); 
}
//--------------------------------------------------------------------
// USER INPUT CONTROL END
//--------------------------------------------------------------------

function Update ()
{
UserInputControl(); // call User Input control inside Update()   
}

Installation:
1. Copy paste the code
2. Call the function – UserInputControl(); – inside Update()

By |Unity3D|Commenti disabilitati su Input Control – Arrow Key – Advanced

Unity – Horizontal and Vertical – Get Axis

Unity – Horizontal and Vertical – Get Axis

Horizontal and Vertical Control – Complete Example

#pragma strict

function Start () {

}

var speed : float = 10.0; // to change speed

function Update () {

 var horiz : float = Input.GetAxis("Horizontal");
 var vert : float = Input.GetAxis("Vertical");

 // Make it move 10 meters per second instead of 10 meters per frame...
 horiz *= Time.deltaTime;
 vert *= Time.deltaTime;
 
 transform.Translate(Vector3(horiz*speed,vert*speed,0));
}
By |Unity3D|Commenti disabilitati su Unity – Horizontal and Vertical – Get Axis

Unity – OnMouse

Unity – OnMouse

OnMouse is called when the user has used the mouse over the GUIElement or Collider.

Basic

1. Create an object
2. Assign a Collider OR ONMOUSE WILL NOT WORK!
3. Attach this script and click over the object with the mouse button:

#pragma strict

function Start ()
{
   
}


function OnMouseDown ()
{
    Debug.Log('Activaction of OnMouseDown!');
}

NOTICE: function OnMouseDown is outside other functions, you do not need put it inside Update() function.

Complete list of OnMouse functions

#pragma strict

function Start ()
{
   
}
// On Mouse Click events ###############################################

function OnMouseDown ()
{
        // When you click over the object
        Debug.Log('Activaction of OnMouseDown!');
}

function OnMouseDrag ()
{
        // When you click over the object and drag
        Debug.Log('Activaction of OnMouseDrag!');
}

function OnMouseUp () {
        // When you release the drag over or outside the object
	Debug.Log('Activaction of OnMouseUp!');
}

function OnMouseUpAsButton () {
        // When you release over the object
	Debug.Log('OnMouseUpAsButton');
}

// On Mouse position cursor events ######################################

function OnMouseEnter () {
	// When cursor enter in the object area
	Debug.Log('Activaction of OnMouseEnter!');
}

function OnMouseOver () {
	// When cursor is over the object area
	Debug.Log('Activaction of OnMouseOver!');
}

function OnMouseExit () {
     // When cursor leave object area
	Debug.Log('Activaction of OnMouseExit!');
}
By |Unity3D|Commenti disabilitati su Unity – OnMouse