Hi
I’m currently making Player sprinting by pressing button.
I made a button on UI and put my script. It doesn’t make any error. But nothing happens.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sprinter : MonoBehaviour {
public float sprintSpeed;
public float coolDownTimer;
public float durationTimer;
private bool sprinting;
void Start(){
sprinting = false;
sprintSpeed = 3.0f;
coolDownTimer = 0;
durationTimer = 0;
}
void OnClick()
{
if(Input.GetButton("Sprint"))
{
if(sprinting){
if(Input.GetButtonDown("Sprint") &&coolDownTimer <= 0)
{
durationTimer = 3.0f;
sprinting = true;
}
durationTimer -= Time.deltaTime;
if(durationTimer >= 0 &&Input.GetButtonDown("Sprint"))
{
transform.position = transform.forward * 3.0f * Time.deltaTime;
}
else
{
transform.position = transform.forward * 1.0f * Time.deltaTime;
}
coolDownTimer -= Time.deltaTime;
if(coolDownTimer <= 0)
{
coolDownTimer = 0;
}
}
else
{
transform.position = transform.forward * 1.0f * Time.deltaTime;
}
}
}
}
I want to make " When you press the sprint button on the UI, the player's speed goes up to 3.0f for 3 second and then have 5 second cool down." My game is FPS. Please help me.