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));
}
}
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.
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
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
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
![](https://koobas.hobune.stream/wayback/20220612061726im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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