- Home /
[SOLVED] Walk, Run and Sprint with single key
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;
}
}
Answer by Trevdevs · Aug 16, 2018 at 10:28 PM
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
}
}
Thank you! Unfortunately nothing is happening. I can walk as normal, but pressing the sprint key does nothing. I tried using a Debug.Log that outputs the speed to see if I just couldn't tell the difference, but it looks like the sprint method is never called.
private void Update ()
{
bool sprinting = false;
//walk
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
{
if(sprinting)
{
humanPlayerRB.velocity = transform.forward * sprintSpeed;
Debug.Log("Speed = " + sprintSpeed); //never enters the consol
}
else
{
humanPlayerRB.velocity = transform.forward * walkSpeed;
Debug.Log("Speed = " + walkSpeed);
}
}
//toggles sprinting
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Q))
{
sprinting = !sprinting;
}
}
I am just glancing over this, getting late here :P BUT
//toggles sprinting
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Q)) // <-- this is in update i think maybe this triggering multiple times ?
{
Debug.Log("Sprint status = " + sprinting );
sprinting = !sprinting;
}
Get key up should only trigger once.
Thanks for stopping by, I need it ;) The debug.log only outputs false; sprinting never ends up equaling true; I don't think it's triggering multiple times, whenever I press it it only outputs one message.
EDIT: Wait! Putting debug.log after sprinting = !sprinting only outputs true values. So... well, I don't know, but I'll keep experimenting.
Whoops I made a mistake in my code that bool should be global not local. Its creating a new variable every frame is setting it to false do this...
public class $$anonymous$$yScript : $$anonymous$$onoBehaviour
{
public int walkSpeed;
public int sprintSpeed;
/Add me
private bool sprinting = false;
void Update()
{
...
}
}
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.Get$$anonymous$$ey($$anonymous$$eyCode.W))
{
if(sprinting)
{
humanPlayerRB.velocity = transform.forward * sprintSpeed;
Debug.Log("Sprinting");
}
else
{
humanPlayerRB.velocity = transform.forward * walkSpeed;
Debug.Log("Walking");
}
}
//toggles sprinting
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Q))
{
//This acts as a toggle statement setting the bool to not what is currently is
sprinting = !sprinting;
}
}
Your answer
Follow this Question
Related Questions
Double Tap to Run 2 Answers
Slow down and play walk animation on left shift? 3 Answers
Animation problem (noob) 1 Answer
Character controller with key click to move forward 0 Answers
How to make different walk sounds 2 Answers