Unity3D – Materials – SetTextureScale – JavaScript

Unity3D – Materials – SetTextureScale – JavaScript

Create a Plane with
– Material with a _MainTex (if you use a diffuse material it is the main color texture)
– TextureScale.js


#pragma strict

function Start () {
        // setup Main Texture (scaleX,scaleY) 
	renderer.material.SetTextureScale ("_MainTex", Vector2(0.5,1));

}

function Update () {

}

Play to see the final result

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Materials – SetTextureScale – JavaScript

Unity3D – Mouse Buttons Codes

Unity3D – Mouse Buttons Codes


        // Detects clicks from the mouse and prints a message
	// depending on the click detected.

	function Update() {
		if(Input.GetMouseButtonDown(0))
			Debug.Log("Pressed left click.");

		if(Input.GetMouseButtonDown(1))
			Debug.Log("Pressed right click.");

		if(Input.GetMouseButtonDown(2))
			Debug.Log("Pressed middle click.");
	}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Mouse Buttons Codes

Unity3D Game Engine – Get Mouse Button – Physic.Raycast – Destroy GameObject

Unity3D Game Engine – Get Mouse Button – Physic.Raycast – Destroy GameObject

Destroy Every ‘hit’ GameObject!

Create a scene with:

– Main Camera
– Cube
– Empty Object, assign Destroy.js:


#pragma strict


function Update(){
    // if you press Mouse Button
    if (Input.GetMouseButtonDown(0)){ // if you press Left Mouse Button -> GetMouseButtonDown(0) 
        var hit: RaycastHit;
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition); // it sends a ray from Camera.main
        if (Physics.Raycast(ray, hit)){ // if it hits something         
       
            Destroy(hit.transform.gameObject); // destroy every hit object
        }
    }
 
}// End Update

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D Game Engine – Get Mouse Button – Physic.Raycast – Destroy GameObject

Unity3D – GetMouseButton – PhysicRaycast – Get GameObject script Component

Unity3D – GetMouseButton – PhysicRaycast – Get GameObject script Component

Create a scene with:

– Cube, attach the script ‘BoomScript.js’


#pragma strict

function Start () {
}

function Update () {
}

function Boom(){
Debug.Log("Kaboooom!");
}

– Main Camera, attach the script ‘Raycast.js’


#pragma strict


function Update(){
    // if you press Mouse Button
    if (Input.GetMouseButtonDown(0)){ // if you press Left Mouse Button -> GetMouseButtonDown(0) 
        var hit: RaycastHit;
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition); // it sends a ray from Camera.main
        if (Physics.Raycast(ray, hit)){ // if it hits something          
          
            // Get Script attached to the GameObject 
            var boomScript : BoomScript = hit.collider.GetComponent(BoomScript);

    		if (boomScript != null) {// if it exists
        	boomScript.Boom(); // execute the function Boom() of BoomScript
   	    	}
        }
    }
 
}// End Update()

Play, click over the Cube, in the console the message will be ‘Kaboooom!’.

For italian people: come funziona?

1. Se viene cliccato il tasto sinistro del mouse

2. parte un raggio dalla Main Camera

3. se colpisce qualcosa ottieni il Component ‘BoomScript’, cioè lo script BoomScript.js

4. se non è nullo, cioè esiste, avvia all’interno di BoomScript.js la funzione Boom()

5. function Boom() manda il messaggio ‘Kaboooom!’ alla console di Unity3D.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – GetMouseButton – PhysicRaycast – Get GameObject script Component

Unity3D – GetMouseButton – PhysicRaycast – Get GameObject tag

Unity3D – GetMouseButton – PhysicRaycast – Get GameObject tag

Once you have determined that your raycast actually hit something, you can query the name of your ‘hit’ object.

Create a scene with:

– Cube, tag it ‘Player’

– Main Camera, attach the script:


#pragma strict


function Update(){
    // if you press Mouse Button
    if (Input.GetMouseButtonDown(0)){ // if you press Left Mouse Button -> GetMouseButtonDown(0) 
        var hit: RaycastHit;
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition); // it sends a ray from Camera.main
        if (Physics.Raycast(ray, hit)){ // if it hits something          
           
            var MyObjectTag = hit.collider.tag; // get the tag of the 'hit' object
            
            Debug.Log(MyObjectTag); // the name of the clicked object
            // Do something...
        }
    }
 
}

Play, if you click the Cube you will see inside console ‘Player’.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – GetMouseButton – PhysicRaycast – Get GameObject tag