Hi everyone I’m new to unity and i’m trying to understand how I can implement a sprint feature in my script. I have a basic walking script using GetAxis and I would like to understand how I would accomplish registering a double tap input button. I’ve experimented with a few code snippets but none of have seem to work for me however I also feel I am not understanding the flow of operation and I believe thats where I’m messing up. If someone could look at my code and point me in the right direction or help me to understand why my code has not been working.
Currently when I execute my code the debug shows both Walking and Running have been activated when I move, if I continue to hold my movement button I then default to Walking only
using UnityEngine;
public class Script : MonoBehaviour
{
public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public float lastTapTime = 0;
public float tapSpeed = 0.5f;
private Vector3 moveDirection = Vector3.zero;
void Start()
{
lastTapTime = 0;
}
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDirection = new Vector3(-Input.GetAxis("Vertical"), 0, Input.GetAxis("Horizontal"));
if (Input.GetAxis("Vertical")* Time.deltaTime != 0 || Input.GetAxis("Horizontal")* Time.deltaTime != 0)
{
Debug.Log("Walking");
if ((Time.time - lastTapTime) < tapSpeed)
{
Debug.Log("Running");
}
}
else
{
Debug.Log(("Not Walking");
lastTapTime = Time.time;
}
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
Debug.Log("Jump");
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}