- Home /
Make a sprint with time respawn
I have this code to my sprint function but when i run and the time is out i don't know how restart the time and run again the function to sprint. I need to know how to put a loop in this code.
float sprintTime;
if (Input.GetKey(KeyCode.LeftShift))
{
sprintTime += 1 / (1 / Time.deltaTime);
}
if (Input.GetKeyDown(KeyCode.LeftShift) && sprintTime <= 2f)
{
walkSpeed = 5.5f;
}
if (Input.GetKeyUp(KeyCode.LeftShift) || sprintTime > 2f)
{
walkSpeed = 2.5f;
}
Answer by Pokedlg3 · Dec 21, 2020 at 08:06 PM
as I understand it, after sprintTime reaches 2, time runs out. However, time is increasing and not decreasing, so I think this resolves:
float sprintTime;
if (Input.GetKey(KeyCode.LeftShift))
{
sprintTime += 1 / (1 / Time.deltaTime);
}
if (Input.GetKeyDown(KeyCode.LeftShift) && sprintTime <= 2f)
{
walkSpeed = 5.5f;
}
if (Input.GetKeyUp(KeyCode.LeftShift) || sprintTime > 2f)
{
sprintTime = 0f;
walkSpeed = 2.5f;
}
yes, it resolved but when the cycle ends of the sprint i need to put a limit of time to try to sprint again. its possible with delta time?
Do you want to set a limit of 2, and when you reach the limit, do you want it to start counting again?
I want to limit the sprite when use the key shift, because if I push shift intermittent the player runs always.
Your answer
Follow this Question
Related Questions
creating prefabs in array 2 Answers
Can you trick a Ui Button that it is being clicked with code? 3 Answers
WaitForSeconds - Shooting Script 3 Answers
Helicopter Controls 1 Answer
got an error Object reference not set to an instance of an object 0 Answers