Documento Sin Título
Documento Sin Título
Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
enum State
{
Patrolling,
Chasing,
Attacking
}
State currentState;
NavMeshAgent enemyAgent;
void Awake()
{
enemyAgent = GetComponent<NavMeshAgent>();
}
void Start()
{
currentState = State.Patrolling;
}
void Update()
{
switch (currentState)
{
case State.Patrolling:
Patrol();
break;
case State.Chasing:
Chase();
break;
case State.Attacking:
Attack();
break;
}
}
void Patrol()
{
if(OnRange())
{
currentState = State.Chasing;
}
if(enemyAgent.remainingDistance<0.5f)
{
GoNextPoint();
}
}
void Chase()
{
enemyAgent.destination = playerTransform.position;
if(OnRange() == false)
{
currentState = State.Patrolling;
}
if(OnRangeAttack() == true)
{
currentState = State.Attacking;
}
void GoNextPoint()
{
if(points.Length == 0)
{
return;
}
void Attack()
{
Debug.Log("Attack");
currentState = State.Chasing;
}
bool OnRangeAttack()
{
RaycastHit hit;
if(Physics.Raycast(transform.position, directionToPlayer,
out hit, distanceToPlayer))
{
if(hit.collider.CompareTag("Player"))
{
lastTargetPosition = playerTransform.position;
return true;
}
}
return false;
}
return false;
}
bool OnRange()
{
if(playerTransform.position == lastTargetPosition)
{
return true;
}
RaycastHit hit;
if(Physics.Raycast(transform.position, directionToPlayer,
out hit, distanceToPlayer))
{
if(hit.collider.CompareTag("Player"))
{
lastTargetPosition = playerTransform.position;
return true;
}
}
return false;
}
return false;
}
void OnDrawGizmos()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireCube(patrolAreaCenter.position, new
Vector3(patrolAreaSize.x,0, patrolAreaSize.y));
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, visionRange);
Gizmos.color = Color.green;
Vector3 fovLine1 = Quaternion.AngleAxis(visionAngle * 0.5f,
transform.up) * transform.forward * visionRange;
Vector3 fovLine2 = Quaternion.AngleAxis(-visionAngle * 0.5f,
transform.up) * transform.forward * visionRange;
Gizmos.DrawLine(transform.position, transform.position +
fovLine1);
Gizmos.DrawLine(transform.position, transform.position +
fovLine2);
}
void Update()
{
switch (currentState)
{
case AIState.Patrolling:
Patrol();
break;
case AIState.Chasing:
Chase();
break;
case AIState.Attacking:
Attack();
break;
}
}
void Patrol()
{
// Moverse hacia el siguiente punto de patrulla
Vector3 targetPosition =
patrolPoints[currentPatrolIndex].position;
transform.position = Vector3.MoveTowards(transform.position,
targetPosition, patrolSpeed * Time.deltaTime);
void Chase()
{
// Moverse hacia el jugador
transform.position = Vector3.MoveTowards(transform.position,
player.position, chaseSpeed * Time.deltaTime);
void Attack()
{
// Simular ataque (Debug)
Debug.Log("¡Ataque!");
CHATGPT PERSONAJE
using UnityEngine;
using UnityEngine.AI;
void Awake()
{
_playerAgent = GetComponent<NavMeshAgent>();
}
void SetDestination()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
_playerAgent.SetDestination(hit.point); // Cambiado a
SetDestination para suavizar el movimiento
}
}
}