Play animation on collision
Hi! I am making a Horror game >:D
I want an animation to play when the Player hits the collider. Actually, the object with the trigger is a child of the object WITH the animation (See the image) this doesn't matter, right? Anyway...
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);
}
}
}
triggerinprefab.png
(71 B)
Comment
Answer by vintar · Jul 31, 2016 at 09:39 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);
}
}
}