unity3d

Unity – Object follow cursor pointer

Unity – Object follow cursor pointer

DRAG AND DROP this script over an Object inside Hierarchy

#pragma strict

function Start () 
{ 
 Screen.showCursor = false; 
} 

// -------------------------------
// FOLLOW MOUSE POSITION START
// -------------------------------
public var depth = 10.0; 

function FollowMousePosition()
{ 
 var mousePos = Input.mousePosition; 
     var wantedPos = Camera.main.ScreenToWorldPoint (Vector3 (mousePos.x, mousePos.y, depth)); 
     transform.position = wantedPos; 
}
// -------------------------------
// FOLLOW MOUSE POSITION END
// -------------------------------

function Update () 
{ 
 FollowMousePosition();  
}

NOTICE: mousePos.x – mousePos.y

By |Unity3D|Commenti disabilitati su Unity – Object follow cursor pointer

Unity – LookAt 2D – Mouse Position

Unity – LookAt 2D – Mouse Position

Instead of ‘Transform.LookAt’ Unity function you can use Mathematic.

1. DRAG AND DROP this script over the object you want to look at target

var mouse_pos : Vector3;
var target : Transform; //Assign to the object you want to rotate
var object_pos : Vector3;
var angle : float;
 
function Update ()
{
    mouse_pos = Input.mousePosition;
    mouse_pos.z = 5.23; //The distance between the camera and object
    object_pos = Camera.main.WorldToScreenPoint(target.position);
    mouse_pos.x = mouse_pos.x - object_pos.x;
    mouse_pos.y = mouse_pos.y - object_pos.y;
    angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(Vector3(0, 0, angle));
}

2. Inspector> select the target for the variable ‘Target’
3. Play!

By |Unity3D|Commenti disabilitati su Unity – LookAt 2D – Mouse Position

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