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 /
  • Help Room /
avatar image
0
Question by milan416 · Jan 04, 2016 at 11:11 PM · animationboolgetkeydownfalsetrue

Making bool true on GetKeyDown and false once animation is done

hey i was wondering how i could get my "Slide" animation to start on DownArrow and stop after the animation is done. I was thinking of adding a delay, but i have another running animation always running when grounded so Thread.sleep won't work. ps. i'm new to unity and scripting so please go into detail.

 Vector3 velocity = Vector3.zero;
 public float JumpForce;
 public float forwardSpeed;
 private Rigidbody2D myRigidbody;
 public bool Slide;
 public bool Grounded;
 public LayerMask whatIsGround;
 public Transform GroundCheck;
 public float GroundCheckRadius;
 
 private Animator MyAnimator;
 
 
 // Use this for initialization
 void Start()
 {

     myRigidbody = GetComponent<Rigidbody2D>();
     
     MyAnimator = GetComponent<Animator> ();
 }
 
 // Do Graphic & Input updates here
 void Update()
 {
     Grounded = Physics2D.OverlapCircle (GroundCheck.position, GroundCheckRadius, whatIsGround);
     
     myRigidbody.velocity = new Vector2 (forwardSpeed, myRigidbody.velocity.y);
     
     if (Input.GetKeyDown (KeyCode.UpArrow) || Input.GetMouseButtonDown (0)) {
         if (Grounded) {
             myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, JumpForce);
         }
     }

     MyAnimator.SetBool ("Grounded", Grounded);



     if (Input.GetKeyDown (KeyCode.DownArrow) || Input.GetMouseButtonDown (1)) 
     {
         if (Grounded) {

         }

     }



 }
 
Comment
Add comment · Show 6
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 incorrect · Jan 04, 2016 at 11:12 PM 0
Share

Have you considered adding method to your script that sets your bool to false and calling it via event in animation as it ends?

avatar image milan416 incorrect · Jan 05, 2016 at 06:00 AM 0
Share

public AnimationEvent Sliding() { $$anonymous$$yAnimator.SetBool ("Slide", false); } this is what i came up with but it says it doesn't return a value, do you know what i would have to add?

avatar image incorrect milan416 · Jan 05, 2016 at 06:41 AM 0
Share

I don't really understand what you want to get. Please, describe how it should work in general. So you have a player, he's running and you want him to?..

avatar image milan416 incorrect · Jan 06, 2016 at 02:35 AM 0
Share

thank you, worked like a charm.

avatar image incorrect milan416 · Jan 06, 2016 at 02:37 AM 0
Share

So mark this answer as a correct, please. Good luck!

avatar image ShadyProductions · Jan 04, 2016 at 11:26 PM 0
Share

You know you can set the triggers in mecanim, so you can use GetComponent().SetBool("whatever", true) and you can link it up with your slide animation when its true, it switches from grounded to slide, and when the slide is done it switches over to grounded again. you can do this all in mecanim in the blend tree states.

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by LMan · Jan 05, 2016 at 12:22 AM

You can get the current animator state, and access the normalized time variable to tell how far along the state is. (0 being the start, 1 being the end.) bear in mind that a transition may prevent a state from reaching 1, so you may have to check for something close to the end, like .95.

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 incorrect · Jan 05, 2016 at 12:25 AM 0
Share

so you may have to check for something close to the end, like .95.

And if the value will change from 0.94 to 0 (i.e. new animation starts) you event not happens. That's not how you should treat floats. Unity provides several approaches to solve this task. Your method is not a best practice.

avatar image LMan · Jan 05, 2016 at 12:16 PM 0
Share

All it requires is looking at the transition length in mecanim to get the exact value reached. This method is viable and in common use.

avatar image incorrect LMan · Jan 06, 2016 at 01:27 AM 0
Share

Can you please provide some examples when this approach is viable?

avatar image LMan incorrect · Jan 06, 2016 at 02:32 AM 1
Share

This question mentions the normalized time method twice.

You will also find this method in the $$anonymous$$ecanim example package from Unity. (match target example).

The duration of a transition is not a dynamic number, you can know exactly how long a transition will take, and what frame it will begin on, and therefore you can check normalized time for the last frame. This method is not as flexible as events, because lengthening the transition can break it if you don't change the float you're looking for. Still it can be simpler and faster to implement.

This is a common practice and I have found it in multiple popular and well reviewed asset store packages, I've also used it myself.

avatar image
0

Answer by incorrect · Jan 06, 2016 at 01:25 AM

Ok. So to make what I've suggested you can add a simple public method to a script you want to switch bool in.

 public void SwitchMyBool()
 {
     myBool = false; //or true, or whatever you need here to be done
 }

And after that you can call this method from animation as it is described in Using Animation Events page of Manual.

Will it work for you?

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

6 People are following this question.

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

Related Questions

Animation Problem 0 Answers

If statement not working 1 Answer

Changing eye texture using bools 0 Answers

Animation won't stop (Solved) 2 Answers

Assets/health.cs(11,23): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration 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