Как убрать поворот игрока в сторону второго игрока?
Проблема, когда игрок получает урон, он начинает бежать быстрее Boost, а когда он стоит на месте и получает урон, а также касаясь Охотника, то он поворачивается к охотнику и начинает бежать на месте. Также, если я направлю игрока в сторону охотника, то при столкновении и нанесении урона, игрок также начинает поворачиваться и бежать в сторону охотника.
Код ниже -
using System.Collections;
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";
// Your variables
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 = 2;
private int currentHealth;
[SerializeField] Text hpText;
[Header("Fail Screen UI")]
[SerializeField] RawImage FailImage;
[SerializeField] Text YouDieAboutText;
[SerializeField] Text YouDieText;
[SerializeField] Text YouDieLVLAboutText;
[SerializeField] Text YouDieLVLText;
[SerializeField] Text DieScoreMoney;
[SerializeField] Button MenuButton;
[Header("Win Screen UI")]
[SerializeField] RawImage WinImage;
[SerializeField] Text YouWinAboutText;
[SerializeField] Text YouWinText;
[SerializeField] Text YouWinLVLAboutText;
[SerializeField] Text YouWinLVLText;
[SerializeField] Text WinScoreMoney;
[SerializeField] Button MenuWinButton;
[SerializeField] float lookRotationSpeed = 8.0f;
private bool isRunning = false;
void Awake()
{
agent = GetComponent<NavMeshAgent>();
animator = GetComponent<Animator>();
playerAnimator = GetComponent<Animator>();
// Initialize custom actions
input = new CustomAction();
AssignInputs();
}
void AssignInputs()
{
// Assign handlers to actions
input.Main.Move.performed += ctx => ClickToMove();
}
void ClickToMove()
{
// Handle mouse click movement
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100, clickableLayers))
{
agent.destination = hit.point;
// Play a click effect if available
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()
{
// Initial setup at the start of the game
currentHealth = maxHealth;
UpdateHPText();
FailImage.gameObject.SetActive(false);
MenuButton.gameObject.SetActive false);
YouDieAboutText.gameObject.SetActive(false);
YouDieText.gameObject.SetActive(false);
YouDieText.gameObject.SetActive(false);
YouDieText.gameObject.SetActive(false);
DieScoreMoney.gameObject.SetActive(false);
initialSpeed = agent.speed;
isBoosting = false;
boostDuration = 0f;
// Generate random values for death score and level
int randomDieScore = Random.Range(10, 15);
DieScoreMoney.text = randomDieScore.ToString();
int randomYouDieLVL = Random.Range(37, 53);
YouDieLVLText.text = randomYouDieLVL.ToString();
// Initial setup for win screen elements
WinImage.gameObject.SetActive(false);
MenuWinButton.gameObject.SetActive(false);
YouWinAboutText.gameObject.SetActive(false);
YouWinText.gameObject.SetActive(false);
YouWinText.gameObject.SetActive(false);
YouWinText.gameObject.SetActive(false);
WinScoreMoney.gameObject.SetActive(false);
// Generate random values for win score and level
int randomWinScore = Random.Range(40, 75);
WinScoreMoney.text = randomWinScore.ToString();
int randomYouWinLVL = Random.Range(37, 53);
YouWinLVLText.text = randomYouWinLVL.ToString();
}
void Update()
{
// Update character's facing and animations
FaceTarget();
SetAnimations();
if (isRunning)
{
animator.Play(WALK);
}
if (isBoosting)
{
boostDuration -= Time.deltaTime;
if (boostDuration <= 0f)
{
isBoosting = false;
SetSpeed(initialSpeed);
}
}
if (currentHealth <= 0)
{
// Play death animation if health reaches zero
playerAnimator.Play(DEATH);
this.enabled = false;
}
}
void FaceTarget()
{
// Rotate the character to face the target
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()
{
// Control movement animations
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)
{
// If health is zero, start a coroutine to show the fail screen objects after a delay
StartCoroutine(ShowObjectsAfterDelay(2f));
}
}
if (other.CompareTag("WinLine"))
{
// If the character reaches the win point, display the win screen and disable this script
WinImage.gameObject.SetActive(true);
MenuWinButton.gameObject.SetActive(true);
YouWinAboutText.gameObject.SetActive(true);
YouWinText.gameObject.SetActive(true);
YouWinText.gameObject.SetActive(true);
YouWinText.gameObject.SetActive(true);
WinScoreMoney.gameObject.SetActive(true);
this.enabled = false;
}
}
IEnumerator ShowObjectsAfterDelay(float delay)
{
// Coroutine to delay the appearance of objects on the fail screen
yield return new WaitForSeconds(delay);
FailImage.gameObject.SetActive(true);
MenuButton.gameObject.SetActive(true);
YouDieAboutText.gameObject.SetActive(true);
YouDieText.gameObject.SetActive(true);
YouDieLVLAboutText.gameObject.SetActive(true);
YouDieLVLText.gameObject.SetActive(true);
DieScoreMoney.gameObject.SetActive(true);
}
public void ApplySpeedBoost()
{
// Apply speed boost
isBoosting = true;
boostDuration = maxBoostDuration;
SetSpeed(initialSpeed + speedBoost);
}
private void SetSpeed(float speed)
{
agent.speed = speed;
}
private void UpdateHPText()
{
// Update the health level text
hpText.text = "HP: " + currentHealth;
}
}
Буду готов даже закинуть на мак если поможете!!!