- Home /
Question by
Zitoox · Aug 01, 2016 at 05:29 PM ·
animationcollisionontriggerenterplay
On collision play animation
Hi! I am making a Horror game >:D
I want an animation to play when the Player hits the collider. When the player collide with my trigger, i want a animation to play. I am using an animator, but i don't know what to do. My script is in C# and it only play the animation when a certain key is pressed, not in collision. So, what do i need to do/change? Here's my current script:
using UnityEngine;
using System.Collections;
public class Scarytest : MonoBehaviour
{
public KeyCode MyKey;
public string MyTrigger;
void Update()
{
if (Input.GetKey(MyKey))
{
GetComponent<Animator>().SetTrigger(MyTrigger);
}
}
}
Comment
Best Answer
Answer by vintar · Aug 01, 2016 at 08:33 PM
public class Scarytest : MonoBehaviour
{
Animator _anim;
public string MyTrigger;
void Awake()
{
_anim = GetComponent<Animator>();
}
void OnTriggerEnter(Collider col)
{
if(col.transform.CompareTag("MyPlayersTag"))
{
_anim.SetTrigger(MyTrigger);
}
}
}