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 verschurengiovanni · Feb 08, 2021 at 11:11 PM · scripting problemscript.scripting beginner

Wait for animation to finish

Hi!

I have a script for a funfair game i'm working on, and I cant figure out how can let the animation fully finish before the user can press the same key again.

I hope someone can help me with this.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using System;
 
 public class SafetyBar : MonoBehaviour
 {
     public Animator animator;
     public bool IsClosed { get; private set; } = true;
 
     public Gondola gondola;
     public Arm arm;
     public Floor floor;
     public Text label;
 
 
     private float exectureActionTime = 0;
     private Action scheduledAction = null;
     private float lastAnimation = 0;
     void Start()
     {
         animator = GetComponent<Animator>();
     }
 
     void Update()
     {
         if (label != null)
             label.text = IsClosed ? "closed" : "open";
 
         if (Input.GetKeyDown(KeyCode.B))
         {
             if (!IsClosed || (!floor.IsDown && !arm.IsMoving() && !gondola.IsMoving()))
             {
                 exectureActionTime = Time.time + UnityEngine.Random.Range(0.0f, 1.0f);
                 scheduledAction = () =>
                 {
                             IsClosed = !IsClosed;
                     lastAnimation = Time.time;
 
                     if (IsClosed)
                     {
                         animator.Play("BeugelOmlaag");
                     }
                     else
                     {
                         animator.Play("Beugel");
 
                     }
                 };
             }
         }
 
         if (exectureActionTime != 0 && scheduledAction != null && Time.time > exectureActionTime)
         {
             scheduledAction();
             exectureActionTime = 0;
             scheduledAction = null;
         }
     }
 }
 
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

Answer by LoloBad · Feb 09, 2021 at 09:50 AM

Why not using an Animaton Event at the very last frame of the animation ?

When you trigger the animation you "lock" your GameObject, at the end of the animation the Animation Event unlock the object.

The Update() method should look like :

 void Update()
 {
        if (!locked)
        {
               if (Input.GetKey(KeyCode.Space))
               {
                      GetComponent<Animator>().SetTrigger("my-animation");
               }
        }
 }

And the Animtion Event is : alt text

Where the Unlock method is:

 public void Unlock ()
 {
         locked = false;
 }

capture.png (6.7 kB)
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 RodrigoAbreu · Feb 08, 2021 at 11:16 PM

Hi @verschurengiovanni, you can use animation events for that. https://docs.unity3d.com/Manual/script-AnimationWindowEvent.html

Or you can manually create wait in a coroutine:

 yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length+anim.GetCurrentAnimatorStateInfo(0).normalizedTime);
 
 
 

Comment
Add comment · Show 4 · 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 verschurengiovanni · Feb 08, 2021 at 11:23 PM 0
Share

I'm not sure how I can implement this correctly, still learning :)

avatar image RodrigoAbreu verschurengiovanni · Feb 08, 2021 at 11:26 PM 0
Share

Gotcha, check it out here in this example. https://www.youtube.com/watch?v=INJmqquHaz4

avatar image verschurengiovanni RodrigoAbreu · Feb 08, 2021 at 11:49 PM 0
Share

Alright, so I got this now

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using System;
 
 public class SafetyBar : $$anonymous$$onoBehaviour
 {
     public Animator animator;
     public bool IsClosed { get; private set; } = true;
 
     public Gondola gondola;
     public Arm arm;
     public Floor floor;
     public Text label;
 
 
 
     private float exectureActionTime = 0;
     private Action scheduledAction = null;
     private float lastAnimation = 0;
     void Start()
     {
         animator = GetComponent<Animator>();
     }
 
     void Update()
     {
 
         if (label != null)
             label.text = IsClosed ? "closed" : "open";
 
         if (Input.GetKeyDown(KeyCode.B))
         {
             if (!IsClosed || (!floor.IsDown && !arm.Is$$anonymous$$oving() && !gondola.Is$$anonymous$$oving()))
             {
                 exectureActionTime = Time.time + UnityEngine.Random.Range(0.0f, 1.0f);
                 scheduledAction = () =>
                 {
                     IsClosed = !IsClosed;
                     lastAnimation = Time.time;
 
                     if (IsClosed)
                     {
                         StartCoroutine(BeugelOmlaag());
                     }
                     else
                     {
                         StartCoroutine(Beugel());
 
                     }
                 };
             }
         }
 
         if (exectureActionTime != 0 && scheduledAction != null && Time.time > exectureActionTime)
         {
             scheduledAction();
             exectureActionTime = 0;
             scheduledAction = null;
         }
 
     }
 
     IEnumerator BeugelOmlaag()
     {
         animator.Play("BeugelOmlaag");
         yield return new WaitForSeconds(animator.GetCurrentAnimatorStateInfo(0).length + animator.GetCurrentAnimatorStateInfo(0).normalizedTime);
     }
 
 
     IEnumerator Beugel()
     {
         animator.Play("Beugel");
         yield return new WaitForSeconds(animator.GetCurrentAnimatorStateInfo(0).length + animator.GetCurrentAnimatorStateInfo(0).normalizedTime);
     }
 
 }
 
 

But I can still press "B" again fore the animation finishes. Am I missing something?

Show more comments

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

229 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 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

Trying to find the highest number than add it to itself. 2 Answers

IEnumerator Loop on keypress 1 Answer

Move from A to B after recieving touch input 1 Answer

Need help fixing my script 1 Answer

Slowly increase motor.force 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