About mobile FPS Sprint
Hello! I'm currently making mobile FPS. I'm trying to make Player run when clicking on the UI button for like 3 seconds. I've got an reply before, but it doesn't seems to work. Here is the script made by him. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Sprinter : MonoBehaviour
{
public Button button; // Drag & Drop the button in the inspector
public float sprintSpeed;
public float sprintDuration;
public float cooldownDuration;
private float sprintTime;
private bool sprinting;
private float currentSpeed ;
private float baseSpeed;
void Start()
{
sprinting = false;
sprintDuration = 3.0f;
cooldownDuration = 5.0f;
baseSpeed = 1.0f;
sprintSpeed = 3.0f;
currentSpeed = baseSpeed;
if( button == null )
button = GameObject.Find("Sprint").GetComponent<Button>();
button.onClick.AddListener(OnClick) ;
}
void Update()
{
if ( sprinting && Time.time > sprintTime + sprintDuration )
{
sprinting = false;
currentSpeed = baseSpeed;
}
transform.Translate( Vector3.forward * currentSpeed * Time.deltaTime );
}
void OnClick()
{
// Prevent the player from spamming the button
if ( Time.time < sprintTime + sprintDuration + cooldownDuration )
return;
sprintTime = Time.time;
sprinting = true;
currentSpeed = sprintSpeed;
}
}
The script doesn't make any error.
I tested this by building apk. It doesn't work for me. Someone please tell me what's the problem. I'm really new to button... I'm making First Person Shooter Game. Not 2D and TPS. Please help me!
Answer by unity_MBL1IWd0bMNS1w · Nov 04, 2020 at 11:13 AM
After i import your script in game, my player is moving automatically forward without any event working then after i disable your script everything working fine but i can't sprint plss help sorry for my bda english
Your answer
Follow this Question
Related Questions
How to create buttons with script? 0 Answers
How do I run a method/behaviour upon GUI button press(onClick)? 0 Answers
Problem with boat script 0 Answers
Detect if UI button clicked 1 Answer