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 Graywolf212 · Jan 26, 2016 at 08:42 AM · c#animationboolean

Bool changes back instantly

I am trying to create a walk-run-sprint system (similar to the one in the fallout series) where your character walks only when caps-lock is toggled, runs by default, and sprints when a key is held down. The bool for checking if the caps lock key is toggled resets less than a second after I press it however. Any/All help is appreciated, thank you!

  using UnityEngine;
     using System.Collections;
     
     public class PlayerController: MonoBehaviour
     {
         public Animator Anim;
         // Use this for initialization
         void Start()
         {
     
         }
     
         // Update is called once per frame
         void Update()
         {
     
             bool toggleCapsLock = false;
     
             if(Input.GetKeyUp(KeyCode.CapsLock)){
                 toggleCapsLock = !toggleCapsLock;
             }
             //Anim.SetFloat("Speed", Input.GetAxis("Vertical")*2);
             Anim.SetBool("Walking", toggleCapsLock);
             Anim.SetFloat("Direction", Input.GetAxis("Horizontal"));
             Anim.SetBool("Running", Input.GetKey(KeyCode.LeftShift));
         }
     }



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
1
Best Answer

Answer by Astraphobia95 · Jan 26, 2016 at 04:34 PM

I haven't scripted Unity in a while but from what I can see you've created your toggle boolean inside the update method, which makes it local to the update method. This means every time update is called (30 times a second?) your boolean is being deleted at the end of the function, and recreated again when it starts (as false, because you've told it to).

Try declaring the boolean as a global variable, just after you initialize the class, something like this:

      public class PlayerController: MonoBehaviour
      {
          bool toggleCapsLock = false;
          public Animator Anim;

          // Use this for initialization
          void Start()
          {
      
          }
      
          // Update is called once per frame
          void Update()
          {
              if(Input.GetKeyUp(KeyCode.CapsLock)){
                  toggleCapsLock = !toggleCapsLock;
              }

              //Anim.SetFloat("Speed", Input.GetAxis("Vertical")*2);
              Anim.SetBool("Walking", toggleCapsLock);
              Anim.SetFloat("Direction", Input.GetAxis("Horizontal"));
              Anim.SetBool("Running", Input.GetKey(KeyCode.LeftShift));
          }
      }

Let me know if that works 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
avatar image
0

Answer by Graywolf212 · Jan 27, 2016 at 12:12 AM

Still not working, here is my code now... Disappointing because I actually understand why that way would make much more sense...

 using UnityEngine;
 [RequireComponent(typeof(Rigidbody))]
 [RequireComponent(typeof(CapsuleCollider))]
 
 
 
 public class PlayerController: MonoBehaviour
 {
     bool toggleCapsLock = false;
     public Animator Anim;
     // Use this for initialization
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if(Input.GetKey(KeyCode.CapsLock)){
             toggleCapsLock = !toggleCapsLock;
         }
         //Anim.SetFloat("Speed", Input.GetAxis("Vertical")*2);
         Anim.SetBool("Walking", toggleCapsLock);
         Anim.SetFloat("Direction", Input.GetAxis("Horizontal"));
         Anim.SetBool("Moving", Input.GetKey(KeyCode.W));
         Anim.SetBool("Sprinting", Input.GetKey(KeyCode.LeftShift));
     }
 }


EDIT: Nvm, see my fix above

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 Graywolf212 · Jan 27, 2016 at 01:02 AM

Ahh, fixed it. All I needed to do was not set it to false, and also check for GetKeyDown instead of GetKey

Comment
Add comment · Show 1 · 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 Astraphobia95 · Jan 27, 2016 at 10:40 AM 0
Share

Great! As a reference for next time you can always declare your variables globally at the start of your class without a value, and then set the initial value inside the start function, as this one only runs once when the script is first initialized.

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

85 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

Related Questions

Help with Enemy death Animation! 1 Answer

Boolean prevent script from working 0 Answers

Awake alternative for UI button (On-Off) 1 Answer

Interchangeable animations in a script? 0 Answers

Looking to activate animation when clicking a button 0 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