How do you play Animation on click in Unity5? Where should you place the script/animation?
Thanks in advance.
How do you play Animation on click in Unity5? Where should you place the script/animation?
Thanks in advance.
@Cybrionowich
Here is a script that will play an animation VIA the animator (more info below)
using UnityEngine;
using System.Collections;
public class MyClass : MonoBehaviour
{
public Camera camera;
private Animator animator;
void Start ()
{
animator = GetComponent<Animator>();
}
void OnMouseDown()
{
if (camera && animator && Input.GetButtonDown("Fire1"))
{
Vector3 mousePos = camera.ScreenToWorldPoint(Input.mousePosition);
Debug.Log("Mosuse is located at Vector3(" + mousePos.x + ", " + mousePos.y + ", " + mousePos.z + ")");
animator.SetTrigger("MyTrigger"); // this requires you to have previously created a trigger in the animator
}
}
}
Once you have created your animation (which going by your question I assume that you have) you need to set up the Animator (you can no longer play animation clips in Unity 5, this will lead to problems). To set up a trigger see my answer here.