Проблема со скриптом игры
Суть такова, что игрок, когда получает урон начинает бежать быстрее и подключается анимация бега, но, если игрок просто встанет и его будут атаковать, то игрок поворачивается в сторону второго игрока, который его бьет и начинает бежать на месте. Вопрос, как это устранить? Пробовал через нейросеть, ничего не вышло. Скрипт ниже -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.AI;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
// константы для имен анимаций
const string IDLE = "m_idle_A";
const string WALK = "m_run";
const string DEATH = "m_death_A";
// ссылки на компоненты
CustomAction input;
NavMeshAgent agent;
Animator animator;
private Animator playerAnimator;
// настройки движения
[Header("Movement")]
[SerializeField] ParticleSystem clickEffect;
[SerializeField] LayerMask clickableLayers;
// настройки ускорения
[Header("Speed Boost")]
[SerializeField] float speedBoost = 2.0f; // цвеличение скорости
[SerializeField] float maxBoostDuration = 7.0f; // максимальная длительность ускорения
private float initialSpeed; // исходная скорость
private bool isBoosting; // флаг ускорения
private float boostDuration; // длительность ускорения
// настройки здоровья
[Header("Health")]
[SerializeField] int maxHealth = 3;
private int currentHealth;
[SerializeField] Text hpText;
[SerializeField] Button restartButton;
[SerializeField] float lookRotationSpeed = 8.0f;
private bool isRunning = false;
void Awake()
{
agent = GetComponent<NavMeshAgent>();
animator = GetComponent<Animator>();
playerAnimator = GetComponent<Animator>();
input = new CustomAction();
AssignInputs();
}
void AssignInputs()
{
input.Main.Move.performed += ctx => ClickToMove();
}
void ClickToMove()
{
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100, clickableLayers))
{
agent.destination = hit.point;
if (clickEffect != null)
{
Instantiate(clickEffect, hit.point + new Vector3(0, 0.1f, 0), clickEffect.transform.rotation);
}
}
}
void OnEnable()
{
input.Enable();
}
void OnDisable()
{
input.Disable();
}
void Start()
{
currentHealth = maxHealth;
UpdateHPText();
restartButton.gameObject.SetActive(false);
initialSpeed = agent.speed;
isBoosting = false;
boostDuration = 0f;
}
void Update()
{
FaceTarget();
SetAnimations();
if (isRunning)
{
animator.Play(WALK);
}
if (isBoosting)
{
boostDuration -= Time.deltaTime;
if (boostDuration <= 0f)
{
isBoosting = false;
SetSpeed(initialSpeed);
}
}
if (currentHealth <= 0)
{
playerAnimator.Play(DEATH);
this.enabled = false;
}
}
void FaceTarget()
{
// поворот к цели
Vector3 direction = agent.destination - transform.position;
if (direction != Vector3.zero)
{
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * lookRotationSpeed);
}
}
void SetAnimations()
{
if (agent.velocity == Vector3.zero)
{
animator.Play(IDLE);
isRunning = false;
}
else
{
isRunning = true;
}
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Hunter"))
{
if (currentHealth > 0)
{
currentHealth--;
UpdateHPText();
ApplySpeedBoost();
}
if (currentHealth == 0)
{
restartButton.gameObject.SetActive(true);
}
}
if (other.CompareTag("WinLine"))
{
Debug.Log("Вы победили!");
}
}
public void ApplySpeedBoost()
{
isBoosting = true;
boostDuration = maxBoostDuration;
SetSpeed(initialSpeed + speedBoost);
}
private void SetSpeed(float speed)
{
agent.speed = speed;
}
private void UpdateHPText()
{
hpText.text = "HP: " + currentHealth;
}
}