Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Eco-Editor · May 30, 2017 at 10:02 AM · animationanimatornavmeshagent

Animation is repeating - due to script logic

Hello all,

I'm writing a script for when the Avatar is approaching destination it plays "pick" animation (using mecanim). I know that my logic is the problem, and I wonder if there's a way to go around it.

     private void Reach()
     {
         shopper.meshRendererFlag.material.color = Color.yellow;
         shopper.animator.SetBool("Walk", false);
         shopper.animator.SetBool("Idle", true);
         PickupItem();
     }
 
     private void PickupItem()
     {
         if (shopper.navMeshAgent.remainingDistance <= shopper.navMeshAgent.stoppingDistance)
         {
             shopper.animator.SetTrigger("PickNeutral");
         }   
        shopper.StartCoroutine(ReachedTarget());
     }  

Of course the avatar is going to play the animation because the If statement remains true, as it's stays at the same place until the next function, but how can it be fixed?

Thank you!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Ali-hatem · May 30, 2017 at 01:45 PM

 bool should_Play = true;

 void PickupItem(){
 if (shopper.navMeshAgent.remainingDistance <= shopper.navMeshAgent.stoppingDistance) {
         if (should_Play) {
             should_Play = false;
             shopper.animator.SetTrigger ("PickNeutral");
         }
     } else { 
         if (!should_Play)
             should_Play = true;
            }
    }  
 }
   
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
avatar image
0

Answer by FM-Productions · May 30, 2017 at 03:04 PM

The question is , how often is your PickupItem() function executed?

If it is executed more than once in a short time, The trigger will always be set. If the character enters the animation, the trigger may be reset, but it does not matter when you always set it. It means your Pickup animation will always repeat itself, because the trigger is always set to true.

Add a conditional logic that makes sure that PickupItem() can only be called once for an item that is on the ground (or whereever).

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
avatar image
0

Answer by Eco-Editor · May 30, 2017 at 09:13 PM

The PickupItem() function is executed each time the agent reaches the target.

There's this function for animation:

  void Start(){
      animation.wrapMode = WrapMode.Once;
  }

But I need something like that for the animator.

I've implemented the code you've suggested with a little twist:

     private void PickupItem()
     {
         if (shopper.navMeshAgent.remainingDistance <= shopper.navMeshAgent.stoppingDistance)
         {
             if (playAnimation)
             {
                 shopper.animator.SetTrigger("PickNeutral");
                 playAnimation = false;
                 shopper.StartCoroutine(ReachedTarget());
             } 
        }

       else if (!playAnimation)
             {
                 shopper.StartCoroutine(ReachedTarget());
             }
         }   


But the next time I'm reaching the point the animation won't play, so I've added the playAnimation = true; variable at the Coroutine. Thank you for the code and for the guiding remarks!

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

163 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

problem with calculate between navmeshagent dirction and model lookat with animator 0 Answers

2D Animation does not start 1 Answer

AI Animation Relative to its Forward Direction 1 Answer

Animation driven NavMeshAgent with Speed, AngularSpeed and Direction as parameters. 1 Answer

run animation when should idle 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