I am trying to use a collider trigger to trigger a player animation using mecanim.
I am using this script:
function OnTriggerEnter (collision : Collider) {
if (collision.gameObject.tag == "Player"){
collision.gameObject.GetComponent<Animator>().Play("bench sit new");
}
}
I get the error “animation state could not be played because animation could not be found”.
However I can access the animation with a key press using the following code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class animation : MonoBehaviour {
public Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("d"))
{
anim.Play("drunk");
}
if (Input.GetKeyDown("a"))
{
anim.Play("anim_runa_run_left");
}
}
private void FixedUpdate()
{
if (Input.GetKeyUp("7"))
{
anim.Play("easy_FuncyBasic");
}
if (Input.GetKeyUp("2"))
{
anim.Play("breakdance");
}
if (Input.GetKeyUp("3"))
{
anim.Play("hard_BounceStep");
}
if (Input.GetKeyUp("4"))
{
anim.Play("normal_HipCircle");
}
if (Input.GetKeyUp("w"))
{
anim.Play("Grounded");
}
if (Input.GetKeyUp("u"))
{
anim.Play("upset");
}
if (Input.GetKeyUp("5"))
{
anim.Play("hard_BasicTurn");
}
if (Input.GetKeyUp("6"))
{
anim.Play("dance new");
}
if (Input.GetKeyUp("e"))
{
anim.Play("eat2");
}
if (Input.GetKeyUp("l"))
{
anim.Play("laugh");
}
if (Input.GetKeyUp("b") )
{
anim.Play("backflip");
}
if (Input.GetKeyUp("g"))
{
anim.Play("sitting");
}
if (Input.GetKeyUp("1"))
{
anim.Play("bench sit new");
}
if (Input.GetKeyUp("9"))
{
anim.Play("lay down");
}
if (Input.GetKeyUp("d"))
{
anim.Play("drinking");
}
if (Input.GetKeyUp("8"))
{
anim.Play("waterslide");
}
if (Input.GetKeyUp("s"))
{
anim.Play("laydown");
}
if (Input.GetKeyUp("a"))
{
anim.Play("anim_runa_idle_left");
}
}
}
I am using mecanim and my animations are marked “humanoid”. They play with the key press script without issue but within my project I also need collider triggers for some animations. I’ve searched high and low and haven’t found any answers.
I’ve seen some suggest marking the animations as “legacy” but I think that’s old advice for the legacy animation system and the animations won’t play when marked that way.
I’m really desperate for help on this as it’s very important to my project. Please help.
Thanks in advance for any help you can give me.