Skip to content

Instantly share code, notes, and snippets.

@luispedrofonseca
Last active June 27, 2019 15:50
Show Gist options
  • Save luispedrofonseca/67d325fb79ba2cc3d72f985c8e9d068f to your computer and use it in GitHub Desktop.
Save luispedrofonseca/67d325fb79ba2cc3d72f985c8e9d068f to your computer and use it in GitHub Desktop.
PG ESAD - Script to simplify the process of triggering an animation by clicking an object
using UnityEngine;
using UnityEngine.EventSystems;
public class OnClickAnimation : MonoBehaviour, IPointerClickHandler
{
public Animator MyAnimator;
public string TriggerName = "idle";
private void Reset()
{
MyAnimator = GetComponentInChildren<Animator>();
var mainCamera = Camera.main;
if (mainCamera == null)
mainCamera = FindObjectOfType<Camera>();
if(mainCamera == null) return;
var physics2DRaycaster = mainCamera.GetComponentInChildren<PhysicsRaycaster>();
if (physics2DRaycaster == null)
mainCamera.gameObject.AddComponent<PhysicsRaycaster>();
var eventSystem = FindObjectOfType<EventSystem>();
if (eventSystem == null)
{
var evGO = new GameObject("EventSystem");
evGO.AddComponent<EventSystem>();
evGO.AddComponent<StandaloneInputModule>();
}
}
public void OnPointerClick(PointerEventData eventData)
{
if(!string.IsNullOrEmpty(TriggerName))
{
MyAnimator.SetTrigger(TriggerName);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment