Код: Выделить всё
using UnityEngine;
using UnityEngine.AI;
public class Enemy : MonoBehaviour
{
public NavMeshAgent agent;
public Transform player;
public float chaseCooldownTime = 3f; // Adjust this as needed
private bool canChase = true;
private Quaternion targetRotation;
void Update()
{
if (player && canChase)
{
// Set destination for NavMeshAgent
agent.SetDestination(player.position);
// Rotate towards player
RotateTowardsPlayer();
}
}
void LateUpdate()
{
if (player != null && canChase)
{
// Apply rotation to the enemy without changing the pitch or roll
transform.rotation = Quaternion.Euler(targetRotation.eulerAngles.x -90, targetRotation.eulerAngles.y + 90, 0);
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Player"))
{
canChase = false;
Invoke("ResetChase", chaseCooldownTime);
}
}
void ResetChase()
{
canChase = true;
}
void RotateTowardsPlayer()
{
// Calculate direction from enemy to player
Vector3 directionToPlayer = player.position - transform.position;
directionToPlayer.y = 0; // Ignore vertical component
// Calculate rotation to face player
targetRotation = Quaternion.LookRotation(directionToPlayer);
}
}
Вот изображение того, что происходит;
[img]https://i.stack. imgur.com/0qD7h.png[/img]
Подробнее здесь: https://stackoverflow.com/questions/781 ... ue-in-game
Мобильная версия