Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 inianbharathi · Mar 16, 2015 at 11:05 AM · animationgetcomponentscriptingbasicstriggers

Trigger animation

Hi, I am new to unity programming .I have following issue

              -We want enemy object to Play certain animation if it enter trigger of player .
              -Player trigger is detected with debug.Log but the object animation is not .it   keeps playing animation given in update.Thanks in advance

           using UnityEngine;

using System.Collections;

public class test : MonoBehaviour {

 // Update is called once per frame
 void Update () {
 
     GetComponent<Animation>().Play ("Turn");

 }
 public void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Player") 
     {

         GetComponent<Animation>().Stop ("Turn");
         Debug.Log("Trigger");
     }
 }

}

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Wiki

Answer by Okido · Mar 16, 2015 at 11:31 AM

Update is called on every frame, so I think when you're calling the new animation, its just immediately being overwritten by the update.

It might be a quick and easy fix to add a bool, which returns true when the player hits the trigger, and goes back to false when the animation is finished. Then use an if statement in your update to check that the bool is true.

If you have a lot of animations though it might be worth making a State Machine, which can be used to switch animations but its more complicated.

Untested example:

 private bool playTriggerAnimation;               //create a boolean
 
     void Update () {
      if(!playTriggerAnimation)                 //if the boolean is false (the ! prefix is a shorthand way of saying "PlayTriggerAnimation == false"
          GetComponent<Animation>().Play ("Turn");
      
      }
      public void OnTriggerEnter(Collider other)
      {
          if (other.gameObject.tag == "Player") 
          {     
              playTriggerAnimation = true;
              GetComponent<Animation>().Stop ("Turn");
              Debug.Log("Trigger");
              GetComponent<Animation>().Play ("NewAnimationHere");

              if(!GetComponent<Animation>().isPlaying("NewAnimationHere")) //if "NewAnimationHere" is not playing
                 playTriggerAnimation = false;
          }
      }


If you do want to create a state machine to handle multiple animations, theres a great tutorial for it here http://www.3dbuzz.com/training/view/3rd-person-character-system/integrating-maya-characters - starting at no.15 :)

Comment
Add comment · Show 2 · 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 inianbharathi · Mar 16, 2015 at 01:13 PM 0
Share

Thanks Okido, It works fine but there is some error in that last if statement, is it must to return that bool back to false. -> if I remove last if it works.

But I got following error when i dont

         -> unexpected symbol I think you missed a circular bracer 

at last. But when I add it give following error

error CS1955: The member `UnityEngine.Animation.isPlaying' cannot be used as method or delegate

       Ill try doing this using state machines but just want to check this error .Thanks in advance!
   

 
avatar image Okido · Mar 19, 2015 at 11:10 AM 0
Share

Sorry for the slow response, I was waiting until I could check one of my own projects for why that error might be happening. The API was updated with Unity 5 so things that used to work may not anymore.

You could try just using "GetComponent().isPlaying" (ins$$anonymous$$d of GetComponent().isPlaying("NewAnimationHere")) It seems to think the brackets are a method when using "isPlaying" for some reason.

I've had problems checking that animation properties were false, so I've done this ins$$anonymous$$d

 if(GetComponent<Animation>().isPlaying) 
    return;
 else
    playTriggerAnimation = false;

Not sure why, but it tends to work for me. :p A state machine is the best way to go though, good decision! if your problem is solved you can click the little check mark the question as answered :)

avatar image
0

Answer by Margallo · Mar 29, 2017 at 04:39 PM

Hi! if you are using collision this might be the good one!

animator anim;

bool hitplayer;

void Update(){

if(hitplayer){

anim.play("you animation for hitplayer like fight when hit the player"); } }

void OnTriggerEnter2D(Collider2D other){ hitPlayer = false; anim.Play ("fightAnim");

} }

void OnTriggerExit2D(Collider2D enemwalk){

hitPlayer = true;

} its works great with my game!. Hope this help :)

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Can I make animations snap to a frame? 1 Answer

Why isn't the animator getting my values? 0 Answers

Having trouble coding an on/off toggle for an animation in script. Please help! 1 Answer

how to get current gameObject animator triggers? 1 Answer

Problem with Animation triggring 0 Answers


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