i would like a simple C# script (i find javascript rather weird and confusing) for sprinting i know this has been asked many times already but any code i have found before has not worked for me please give me code to learn from that will let my character sprint when i press either shift
Here’s a really simple method to get you in the right direction
if(Input.GetKey(KeyCode.W)){
float speed;
if(Input.GetKey(KeyCode.LeftShift)){
speed=20;
}else{
speed=10;
}
gameObject.transform.position += Vector3.forward * speed * Time.deltaTime;
}
Just place the code above inside the update method in a script that is attached to the “player” object. When you press “W” it will move forward, but when you press “Left Shift” with “W” it will sprint forward.
Hey, I know this is literally three years later, so I hope your problem is sorted by now!
I have found what I consider an improvement on the solution (it helped me a lot)…so I’m going to post this in case anyone comes across this again.
Vector3 movement = Vector3.zero;
if(Input.GetKey(KeyCode.W)){
float speed;
if(Input.GetKey(KeyCode.LeftShift)){
speed=20;
}else{
speed=10;
}
movement += Vector3.forward * speed * Time.deltaTime;
}
Basically what this’ll do is allow you to sprint and move in the direction you are facing…
Yeah, hope this’ll help anyone who comes across this! XD