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 JeevanjotSingh · Jun 18, 2015 at 12:48 PM · unity 5getcomponentplaystopanmation

Another animation is not working

Greetings,

Here is my code -

 using UnityEngine;
 using System.Collections;
 
 public class enemyai : MonoBehaviour {
     public Transform target;
         NavMeshAgent agent;
     // Use this for initialization
     void Start () {
         agent = GetComponent<NavMeshAgent>();
 
     }
     
     // Update is called once per frame
     void Update () {
 
         agent.SetDestination(target.position);
     
 
         GetComponent<Animation>().Play ("Run");
 
         if(Input.GetKeyDown(KeyCode.LeftAlt))
            {
 
             GetComponent<Animation>().Play("Attack_01");
         }
 
     }
     void OnTriggerEnter(Collider hel)
     {
         if(hel.gameObject.tag == "enemy")
         {
             GetComponent<Animation>().Stop("Run");
         }
     }
 
 }
 

It's not playing 2nd animation cause it's not stopping 1st , if i press left alt then it just play it for i millisecond , just a little bit and then again play run animation as loop .

can you please tell me how can i correct this in unity 5.1 , it have GetComponent so a bit confusing for me , can you write that code line to fix this , thanks .

Comment
Add comment · Show 5
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 JeevanjotSingh · Jun 18, 2015 at 07:54 PM 0
Share

just got that , this is the final code , hope it helps you -

 using UnityEngine;
 using System.Collections;
 
 public class enemyai : $$anonymous$$onoBehaviour {
     public Transform target;
         Nav$$anonymous$$eshAgent agent;
     public bool attack=false;
     // Use this for initialization
     void Start () {
         agent = GetComponent<Nav$$anonymous$$eshAgent>();
 
     }
     
     // Update is called once per frame
     void Update () {
 
         agent.SetDestination(target.position);
     if(attack==false)
         {
         GetComponent<Animation>().Play ("Run");
         }
         if(attack==true)
         {
             GetComponent<Animation>().Play("Attack_01");
         }
     }
 
     /*    GetComponent<Animation>().Play ("Run");
 
         if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftAlt))
            {
 
             GetComponent<Animation>().Play("Attack_01");
         }
 
     }*/
     void OnTriggerStay(Collider stay)
     {
         if(stay.gameObject.tag == "areap")
         {
     
                 attack=true;
 
         }
     
     }
     void OnTriggerExit(Collider exit)
     {
         if(exit.gameObject.tag == "areap")
         {
             attack = false;
         }
     }
 
 
     }
 

avatar image Shark-Boy · Jun 19, 2015 at 02:11 AM 0
Share

If your second script works then why not explain what you changed and post as a answer? (you can use convert to answer on your comment)

avatar image JeevanjotSingh · Jun 19, 2015 at 10:33 AM 0
Share

Well @Shark Boy that's good idea, but this are not program$$anonymous$$g noob forums ,where i need to explain each line , you can read then and according to logic , you can simplify that easily .

avatar image Shark-Boy · Jun 19, 2015 at 11:48 AM 0
Share

Yes but changing to a answer cleans up the unanswered questions and makes it so other people can search answers and have this come up. With a solution. No you do not need to go into line by line explanation of what you did I just meant say what was wrong and point out what you changed.

avatar image JeevanjotSingh · Jun 19, 2015 at 06:45 PM 0
Share

@Shark Boy , Yeah sure -

OnTriggerEnter() Function called 1 times in 1 frame and ends up with one call , so animation plays just for 0.02 sec . So animation plays a bit and again play the old one where it starts from , So i just changed it with OnTriggerStay() to play until it's in trigger .Now i delete left alt key command to run animation cause i want to make it Enemy ai , and take one bool to show that if it triggers (I take bool becuase trigger will not run with update function , you know what i meant to say) and then attack is true , then update calls - check attacks true and play attack ins$$anonymous$$d of run and when Triggers exit , attack goes false and then again it will play run , until it will not found trigger .

0 Replies

· Add your reply
  • Sort: 

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

Where i use Getcomponent() here in this script 2 Answers

Choppy Game Screen 3 Answers

How do I acces a specific GameObject within the terrain???? 0 Answers

Unity stops while I work with my project 0 Answers

HasComponent 4 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