indiegamedev

Unity 3D – Animation calls Functions – Animation Events – JavaScript

Unity 3D – Animation calls Functions – Animation Events – JavaScript

Animation Events, which allow you to call functions in the object’s script at specified points in the timeline.

Official docs: http://docs.unity3d.com/Manual/animeditor-AnimationEvents.html

1. Create a Cube

2. Attach Cube.js:

// This JavaScript function can be called by an Animation Event
function PrintFloat (theValue : float) {
    Debug.Log ("PrintFloat is called with a value of " + theValue);
}

3. Select the Cube GameObject, MAIN TOP MENU> Window> Animation> Animation Toolbar>
> Add Keyframe, create your animation
> Add Event, Select a function, setup a value

4. Play

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D – Animation calls Functions – Animation Events – JavaScript

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