• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by matthew77 · Dec 07, 2017 at 05:17 AM · animationmecanim

need help triggering player animation with a collider

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image matthew77 · Dec 07, 2017 at 07:06 AM 0
Share

I'm not sure how I'm supposed to edit it in a more acceptable way. I added paragraph separations that did not show when posted.

I tried to contain the code within code tags as is done on the forums but that did not work either.

avatar image bartbussel · Dec 07, 2017 at 09:08 AM 0
Share

Did you check the is trigger box in the collider?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by bartbussel · Dec 07, 2017 at 09:12 AM

 void OnTriggerEnter (Collider Col) {
     if (Col.gameObject.tag == "Player"){
        Col.gameObject.GetComponent<Animator>().Play("bench sit new");
     }
  }

This should work and make sure your collider is set to is trigger!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image matthew77 · Dec 08, 2017 at 06:57 AM 0
Share

Thanks for the response but this gives me compiler errors.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

78 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can the mecanim animators Controller var be set in code? 1 Answer

Using Override Animation Controllers (NOT WORKING) 0 Answers

Random float parameter for idle animations 1 Answer

How do I get the Root Transform Position (XY) to loop seamlessly? [Unity] 0 Answers

How can I determine when a MecAnim animation is done? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges