unity3d

Unity3D – Texture Mover – Javascript

Unity3D – Texture Mover – Javascript

How to move texture via code.

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

Simple code:


#pragma strict

    // Scroll main texture based on time
	var scrollSpeed : float; // Assign in Inspector, start try 0.5

function Start () {
    // setup Main Texture (scaleX,scaleY) 
    // x = 0.5 perchè la texture che uso è il doppio in larghezza della dimensione del piano
    // il piano è 16:9, ta texture 32:9 per avere un landscape più ampio
	renderer.material.SetTextureScale ("_MainTex", Vector2(0.5,1));
}// END Start()

function Update () {
        var offset : float = Time.time * scrollSpeed;
		renderer.material.SetTextureOffset ("_MainTex", Vector2(offset,0));
}// END Update()

Play to see the final result

Pro code:


#pragma strict

    var isVertical : boolean = false; // check in Inspector if you need a vertical scrool 
    
    var invertDirection : boolean = false; // check in Inspector if you need change scrool direction
    
    // Scroll main texture based on time
	var scrollSpeed : float; // Assign in Inspector, start try 0.5
	
	var scaleX : float; // X scale of the texture
	var scaleY : float; // X scale of the texture

function Start () {

    // setup Main Texture (scaleX,scaleY) START ###############################################
    // ad esempio scaleX = 0.5 se la texture che uso è il doppio in larghezza della dimensione del piano
    // il piano è 16:9, la texture 32:9 
	renderer.material.SetTextureScale ("_MainTex", Vector2(scaleX,scaleY));
	// setup Main Texture (scaleX,scaleY) END #################################################
	
}// END Start()

function Update () {

        MoveTexture(); // devo richiamarla da update perchè si deve aggiornare ad ogni frame
        
}// END Update()

function MoveTexture(){

        var offset : float; // lo spostamento della texture...
        
        // left->right or right->left or up->down or down->up ----------------------------------------------
	    if (invertDirection){ // positivo
	    offset = Time.time * -scrollSpeed; 
	    } 
	    
	     if (!invertDirection){ // negativo
	    offset = Time.time * scrollSpeed; 
	    }
        // vertical or horizontal  -------------------------------------------------------------------------
	    if (isVertical) { // in verticale 
	        renderer.material.mainTextureOffset = Vector2 (0, offset);  
	    }else{  // in orizzontale
	        renderer.material.mainTextureOffset = Vector2 (offset, 0);  
	    } 
  
}// END MoveTexture()


Setup in Inspector the value of variables

Play, you can change values on the fly into Inspector!

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Texture Mover – Javascript

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 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