Unity 3D Game Engine – Play an Animation On Click

1. MAIN TOP MENU> GameObject> Create Other> Cube> Inspector> Reset

2. MAIN TOP MENU> Window> Animation
TOP LEFT of Animation Window> Drop Down Menu ‘Create New Clip’> create ‘Pulse.anim’

3. Select the Cube or others Game Object in the scene.
Non ha importanza cosa scelgo, l’oggetto sarà influenzato al solo scopo di creare le chiavi dell’animazione, NON SARA’ ASSEGNATO ALL’OGGETTO CHE USO IN QUESTO MOMENTO!

4. Animation Window> REC Button> Create your Keys…
Project> select the Animation Clip> Inspector> Wrap Mode> Loop / Once (una volta sola) etc…

Repeat point 2-3 to create ‘Rotate.anim’

5. Hierarchy> select the Cube> Inspector> ‘Add Component’> Miscellaneous> Animation
Ora e stato aggiunto un ‘COMPONENT Animation’

DRAG E DROP da Project:

-‘Rotate.anim’ sopra il primo slot Animation, questa sarà l’animazione di default!

-‘Pulse.anim’ sopra lo slot libero in ‘Animations’, è la lista di tutto gli slot di animazioone disponibili, per aumentarla basta variare il parametro ‘Size’

– Spuntare ‘Play Automatically’

– Cullin Type: Based On Renderers, in questo modo l’animazione sarà calcolata solo se l’oggetto verrà renderizzato.

6. Aggiungere al Cubo lo script OnClick.JS


#pragma strict

function Start () {
	animation.CrossFade("Rotate"); 
	 
}

function OnMouseDown ()
{
        // When you click over the object
        // start the animation
        animation.CrossFade("Pulse");        
}
  
function Update () {


}

L’animazione di default sarà ‘Rotate’ in loop infinito, poi al click del mouse sarà ciclata una volta ‘Pulse’