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 hixon33 · Apr 19, 2015 at 01:22 AM · animatorboolean

Make a boolean positive and then negative with same button

Hey all So I've looked a bit and haven't really found an answer, so here goes. I have a code ( in JS but help in C# works also) that starts an animation when the space key is pressed and I have that part working but I don't know how to make it so that when I press the space key again the animation will stop.

this is the relevant code

 private var anim: Animator;        //a reference to the animator component
  
 function Awake()
 {    
     //set up references
     anim = GetComponent(Animator);
 }
 
 function Update()
 {
 var swing: boolean = Input.GetButton("Attack");
     
     //Animate the player
     Animating(swing);
 }
 function Animating (swing: boolean)
 {
         
     if(swing == true)
     {
         anim.SetBool("Sword01", true);
     }
                                 
 }
 
 

So yeah any help on how to make it set the boolean back to false by pressing the space button a second time would be awesome! The animator is already set up so when the boolean turns false the animation should stop. Thanks

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by jcv8000 · Apr 19, 2015 at 01:29 AM

  function Update()
  {
      if (Input.GetButtonDown("Attack"))
      {
          anim.SetBool("Sword01", !anim.GetBool("Sword01"));
      }
  }
Comment
Add comment · Show 5 · 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 Bunny83 · Apr 19, 2015 at 01:31 AM 1
Share

You might want to use "Input.GetButtonDown" as your code will flip the bool every frame as long as the button is held down.

avatar image hixon33 · Apr 19, 2015 at 01:39 AM 0
Share

Oh yeah I used the GetButtonDown function I guess I missed that in the post. And thank you both very much its working now! Is there any chance you could explain the part of the code? "!anim.GetBool("Sword01"));"

avatar image jcv8000 · Apr 19, 2015 at 01:45 AM 1
Share

Sure. anim.GetBool("Sword01") returns the value of the animator boolean called "Sword01" that you were trying to originally set. Using a "!" means the opposite of that value. So that means it is setting the opposite of what it is.

avatar image jcv8000 · Apr 19, 2015 at 01:46 AM 1
Share

I also changed it to say "GetButtonDown" in the answer

avatar image Lo0NuhtiK · Apr 19, 2015 at 03:25 AM 0
Share

@LoONuhti$$anonymous$$: Yes, that's exactly what it does, it toggles it. "!" means "not"...not false is true, not true is false...... @Eric5h5 Dec 29, 2011 at 03:28 P$$anonymous$$

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

19 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

Related Questions

How to check how many times an animation has played? 1 Answer

Add a bool to animator & activate by key press 1 Answer

how to turn animator on if bool = true 2 Answers

Error : Animator has not been initialized. UnityEngine.Animator:SetBool(String, Boolean) 1 Answer

how do i turn animator on from script if bool is true 2 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