Добавьте новую переменную для отслеживания состояния блокировки:
private bool isLocked = false;
Измените метод PerformAction():
void PerformAction()
{
if (!isLocked)
{
isLocked = true;
DisableMovementForTime(2.5f);
animator.SetTrigger("isfall");
animator.SetTrigger("isgoup");
}
}
Модифицируйте корутину DisableMovementRoutine():
IEnumerator DisableMovementRoutine(float time)
{
canMove = false;
canRun = false;
yield return new WaitForSeconds(time);
canMove = true;
yield return new WaitForSeconds(4f);
canRun = true;
isLocked = false;
}
В методе Update() измените проверку нажатия клавиши:
if (Input.GetKeyDown(keyToHold) && !isLocked)
{
isKeyDown = true;
currentTime = 0.0f;
}
if (isKeyDown && !isLocked)
{
currentTime += Time.deltaTime;
if (currentTime >= holdTime)
{
PerformAction();
isKeyDown = false;
}
}
if (Input.GetKeyUp(keyToHold))
{
isKeyDown = false;
currentTime = 0.0f;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Move : MonoBehaviour
{
public float moveSpeed;
public float rotateSpeed;
private Animator animator;
private bool canMove = true;
private bool canRun = true;
public KeyCode keyToHold;
public float holdTime = 3.0f;
private bool isKeyDown = false;
private float currentTime = 0.0f;
private void Start()
{
animator = GetComponent<Animator>();
Cursor.visible = false;
}
private void Update()
{
if (canRun)
{
if (Input.GetKey(KeyCode.LeftShift))
{
moveSpeed = 10f;
animator.SetBool("issrun", true);
}
else
{
moveSpeed = 5f;
animator.SetBool("issrun", false);
}
}
if (canMove)
{
if (!Input.GetKey(KeyCode.W))
{
animator.SetBool("isrun", false);
}
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
animator.SetBool("isrun", true);
}
if (!Input.GetKey(KeyCode.W))
{
animator.SetBool("isrun", false);
}
PersonRotate();
}
if (Input.GetKeyDown(keyToHold))
{
isKeyDown = true;
currentTime = 0.0f;
}
if (isKeyDown)
{
currentTime += Time.deltaTime;
if (currentTime >= holdTime)
{
PerformAction();
isKeyDown = false;
}
}
if (Input.GetKeyUp(keyToHold))
{
isKeyDown = false;
currentTime = 0.0f;
}
}
void PerformAction()
{
DisableMovementForTime(2.5f);
animator.SetTrigger("isfall");
animator.SetTrigger("isgoup");
}
void DisableMovementForTime(float time)
{
StartCoroutine(DisableMovementRoutine(time));
}
IEnumerator DisableMovementRoutine(float time)
{
canMove = false;
canRun = false;
yield return new WaitForSeconds(time);
canMove = true;
yield return new WaitForSeconds(4f);
canRun = true;
}
void PersonRotate()
{
transform.Rotate(0, Input.GetAxis("Mouse X") * rotateSpeed, 0);
}
}