Я установил журнал отладки, чтобы увидеть, не вызывается ли что-то. Все журналы отображаются в консоли. Враг может поразить игрока, но когда он это делает, он появляется на другой стороне игрока.
https://www.youtube.com/watch?v=0McNnxUVM4o&feature=youtu.be - это видео о том, что происходит.
Я пытаюсь заставить врага подойти к игроку, остановиться, ударить игрока и остаться на одной стороне игрока, а не переходить на другую сторону. Единственный другой скрипт, который я связал с врагом, - это HP. Было бы лучше установить его на OnTriggerEvent
?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(NavMeshAgent))]
[RequireComponent(typeof(Rigidbody))]
public class EnemyController : MonoBehaviour
{
public float movementSpeed;
public GameObject Target;
public float lookRadius = 10f;
// Attack
public int AttackDamageMin;
public int AttackDamageMax;
public float AttackCooldownTimeMain;
public float AttackCooldownTime;
public float minAttackDistance = 10f;
Transform target;
NavMeshAgent agent;
void Start()
{
target = PlayerManager.instance.player.transform;
agent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
float distance = Vector3.Distance(target.position, transform.position);
Debug.Log("Float Dist");
if(distance < minAttackDistance)
{
StopEnemy();
Debug.Log("Enemy Stop");
AttackTarget();
Debug.Log("Player is Attacked");
agent.SetDestination(target.position);
if (distance <= agent.stoppingDistance)
{
Facetarget();
Debug.Log("Enemy is now Facing Player");
}
transform.Translate(Vector3.forward * movementSpeed);
}
else
{
GoToTarget();
Debug.Log("Enemy is now going to Player");
if (AttackCooldownTime > 0)
{
AttackCooldownTime -= Time.deltaTime;
Debug.Log("Attack is now on Cooldown");
}
else
{
AttackCooldownTime = AttackCooldownTimeMain;
}
}
}
void AttackTarget()
{
Target.transform.GetComponent<Player>().TakeDamage(Random.Range(AttackDamageMin, AttackDamageMax));
Debug.Log("Attacking");
}
void GoToTarget()
{
agent.isStopped = false;
agent.SetDestination(target.transform.position);
Debug.Log("Gototarget");
}
void StopEnemy()
{
agent.isStopped = true;
Debug.Log("Stop");
}
void Facetarget()
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, lookRadius);
}
}