I’m trying to create a character controller where I can walk forward using W, and then by pressing another key (say Q,) I can switch to running, and press it again to start walking again. I’d also like to be able to double-tap for sprinting, then press to return to walk/run again. I have forward motion and walk and run by holding Q down, but how do I change it to register double-tapping and changing states with only a single press? here’s my code:
private void Update ()
{
//walk
if (Input.GetKey(KeyCode.W))
{
humanPlayerRB.velocity = transform.forward * walkSpeed;
}
//run
if (Input.GetKey(KeyCode.W) && (Input.GetKey(KeyCode.Q))) //I've tried using GetKeyDown here, to no effect
{
humanPlayerRB.velocity = transform.forward * runSpeed;
}
}
You’re question is confusing and doesn’t provide enough details but if I understand what you’re trying to do here are some code snippets
void Update()
{
bool sprinting = false;
if(Input.GetKey("WalkKey"))
{
if(sprinting)
{
//sprint
}
else
{
//walk
}
}
if(Input.GetKeyDown("RunKey"))
{
sprinting = !sprinting; //This acts as a toggle statement setting the bool to not what is currently is
}
}
Oh that makes sense! Thank you, it’s working now
Here’s my code for any passers-by
[SerializeField] public int walkSpeed;
[SerializeField] public int sprintSpeed;
Rigidbody humanPlayerRB;
private bool sprinting = false;
private void Start()
{
humanPlayerRB = GetComponent<Rigidbody>();
}
// Update is called once per frame
private void Update ()
{
//walk
if (Input.GetKey(KeyCode.W))
{
if(sprinting)
{
humanPlayerRB.velocity = transform.forward * sprintSpeed;
Debug.Log("Sprinting");
}
else
{
humanPlayerRB.velocity = transform.forward * walkSpeed;
Debug.Log("Walking");
}
}
//toggles sprinting
if (Input.GetKeyDown(KeyCode.Q))
{
//This acts as a toggle statement setting the bool to not what is currently is
sprinting = !sprinting;
}
}