0% found this document useful (0 votes)
23 views8 pages

Documento Sin Título

Uploaded by

mariabg16072003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views8 pages

Documento Sin Título

Uploaded by

mariabg16072003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

using System.

Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class IAEnemy1 : MonoBehaviour


{

enum State
{
Patrolling,
Chasing,
Attacking
}

State currentState;

NavMeshAgent enemyAgent;

[SerializeField] Transform playerTransform;

[SerializeField] Transform patrolAreaCenter;


[SerializeField] Vector2 patrolAreaSize;

[SerializeField] float visionRange = 15;

[SerializeField] float rangeAttack = 5;

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;
}

pointDestination = (pointDestination + 1) % points.Length;


enemyAgent.destination = points[pointDestination].position;
}

void Attack()
{
Debug.Log("Attack");
currentState = State.Chasing;
}

bool OnRangeAttack()
{

Vector3 directionToPlayer = playerTransform.position -


transform.position;
float distanceToPlayer = directionToPlayer.magnitude;
float angleToPlayer = Vector3.Angle(transform.forward,
directionToPlayer);

if(distanceToPlayer <= visionRange && angleToPlayer<


visionAngle* 0.3f)
{
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;
}

bool OnRange()
{

Vector3 directionToPlayer = playerTransform.position -


transform.position;
float distanceToPlayer = directionToPlayer.magnitude;
float angleToPlayer = Vector3.Angle(transform.forward,
directionToPlayer);

if(distanceToPlayer <= visionRange && angleToPlayer<


visionAngle* 0.5f)
{

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);
}

CHATGPT SCRIPT ENEMY:


using UnityEngine;

public class EnemyAI : MonoBehaviour


{
public Transform[] patrolPoints; // Puntos de patrulla
public float patrolSpeed = 2f; // Velocidad de patrulla
public float chaseSpeed = 4f; // Velocidad de persecución
public float detectionRange = 10f; // Rango de detección del
jugador
public float attackRange = 2f; // Rango de ataque
public Transform player; // Referencia al jugador
public float attackCooldown = 2f; // Cooldown de ataque
public float nextAttackTime = 0f; // Tiempo para próximo ataque

private enum AIState { Patrolling, Chasing, Attacking }


private AIState currentState = AIState.Patrolling;
private int currentPatrolIndex = 0;

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);

// Si llegamos al punto de patrulla, seleccionamos el siguiente


if (Vector3.Distance(transform.position, targetPosition) <
0.1f)
{
currentPatrolIndex = (currentPatrolIndex + 1) %
patrolPoints.Length;
}

// Verificar si el jugador está dentro del rango de detección


if (Vector3.Distance(transform.position, player.position) <
detectionRange)
{
currentState = AIState.Chasing;
}
}

void Chase()
{
// Moverse hacia el jugador
transform.position = Vector3.MoveTowards(transform.position,
player.position, chaseSpeed * Time.deltaTime);

// Si el jugador está dentro del rango de ataque, atacar


if (Vector3.Distance(transform.position, player.position) <
attackRange)
{
currentState = AIState.Attacking;
}
// Si el jugador está fuera del rango de detección, volver a
patrullar
else if (Vector3.Distance(transform.position, player.position)
> detectionRange)
{
currentState = AIState.Patrolling;
}
}

void Attack()
{
// Simular ataque (Debug)
Debug.Log("¡Ataque!");

// Si es hora de realizar el próximo ataque


if (Time.time >= nextAttackTime)
{
// Realizar el ataque
// ...

// Establecer el tiempo para el próximo ataque


nextAttackTime = Time.time + attackCooldown;
}

// Volver al estado de persecución


currentState = AIState.Chasing;
}
}

CHATGPT PERSONAJE
using UnityEngine;
using UnityEngine.AI;

[RequireComponent(typeof(NavMeshAgent))] // Asegura que el jugador


tenga el componente NavMeshAgent adjunto
public class PlayerController : MonoBehaviour
{
private NavMeshAgent _playerAgent;

void Awake()
{
_playerAgent = GetComponent<NavMeshAgent>();
}

// Update is called once per frame


void Update()
{
if (Input.GetButtonDown("Fire1")) // Cambiado a GetButtonDown
para activar el movimiento solo una vez por clic
{
SetDestination();
}
}

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
}
}
}

You might also like