Pathfinding
Remade prefabs, added scripts for pathfinding through navmesh, rewrote enemy spawn, roomGen and parts of handler
This commit is contained in:
@@ -1,91 +1,257 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
public class EnemyMovement : MonoBehaviour
|
||||
{
|
||||
public CharacterController charControl;
|
||||
|
||||
public float speed = 12f;
|
||||
public float gravity = -9.81f;
|
||||
public float jumpHeight = 3f;
|
||||
|
||||
|
||||
|
||||
public Transform groundCheck;
|
||||
public float groundDistance = 0.4f;
|
||||
public LayerMask groundMask;
|
||||
public LayerMask obstacleMask;
|
||||
public float detectionOffset = 0.5f;
|
||||
|
||||
[Header("Navigation")]
|
||||
public NavMeshAgent agent;
|
||||
public Transform player;
|
||||
public float obstacleDetectionDistance = 1f;
|
||||
public float rotationSpeed = 5f;
|
||||
public float updatePathInterval = 0.5f;
|
||||
public float roomCheckInterval = 2f;
|
||||
|
||||
private Vector3 velocity;
|
||||
private bool isGrounded;
|
||||
[Header("Combat")]
|
||||
public float attackRange = 2f;
|
||||
public float attackCooldown = 1f;
|
||||
public float sightRange = 20f;
|
||||
public float patrolRange = 5f;
|
||||
private bool canAttack = true;
|
||||
|
||||
void Start()
|
||||
private enum EnemyState { Patrolling, Chasing, Attacking }
|
||||
private EnemyState currentState = EnemyState.Patrolling;
|
||||
private Vector3 patrolCenter;
|
||||
private Vector3 patrolTarget;
|
||||
|
||||
[Header("Animation")]
|
||||
public Animator animator;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// Najdi hr<68><72>e
|
||||
GameObject playerObject = GameObject.FindWithTag("Player");
|
||||
if (playerObject != null)
|
||||
{
|
||||
player = playerObject.transform;
|
||||
}
|
||||
else
|
||||
|
||||
// Nastav v<>choz<6F> pozice
|
||||
patrolCenter = transform.position;
|
||||
GenerateNewPatrolTarget();
|
||||
|
||||
// Spustit coroutines
|
||||
if (agent != null)
|
||||
{
|
||||
Debug.LogError("Player not found in the scene! Ensure the player GameObject is tagged as 'Player'.");
|
||||
agent.stoppingDistance = attackRange - 0.2f;
|
||||
StartCoroutine(UpdatePath());
|
||||
StartCoroutine(CheckPlayerInRange());
|
||||
StartCoroutine(CheckCurrentRoom());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Update()
|
||||
private void Update()
|
||||
{
|
||||
if(player == null) return;
|
||||
if (agent == null) return;
|
||||
|
||||
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
|
||||
|
||||
if(isGrounded && velocity.y < 0)
|
||||
// Update animac<61>
|
||||
if (animator != null)
|
||||
{
|
||||
velocity.y = -2f;
|
||||
animator.SetFloat("Speed", agent.velocity.magnitude);
|
||||
}
|
||||
|
||||
// Stavov<6F> chov<6F>n<EFBFBD>
|
||||
switch (currentState)
|
||||
{
|
||||
case EnemyState.Patrolling:
|
||||
PatrolBehavior();
|
||||
break;
|
||||
case EnemyState.Chasing:
|
||||
ChaseBehavior();
|
||||
break;
|
||||
case EnemyState.Attacking:
|
||||
AttackBehavior();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void PatrolBehavior()
|
||||
{
|
||||
// Pokud je c<>l patrolov<6F>n<EFBFBD> dosa<73>en, vyber nov<6F> c<>l
|
||||
if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
|
||||
{
|
||||
GenerateNewPatrolTarget();
|
||||
}
|
||||
}
|
||||
|
||||
private void ChaseBehavior()
|
||||
{
|
||||
if (player == null)
|
||||
{
|
||||
currentState = EnemyState.Patrolling;
|
||||
return;
|
||||
}
|
||||
|
||||
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
|
||||
|
||||
if (distanceToPlayer <= attackRange)
|
||||
{
|
||||
currentState = EnemyState.Attacking;
|
||||
agent.isStopped = true;
|
||||
}
|
||||
else if (distanceToPlayer > sightRange * 1.5f)
|
||||
{
|
||||
// Hr<48><72> je p<><70>li<6C> daleko, vra<72> se k patrolov<6F>n<EFBFBD>
|
||||
currentState = EnemyState.Patrolling;
|
||||
GenerateNewPatrolTarget();
|
||||
}
|
||||
}
|
||||
|
||||
private void AttackBehavior()
|
||||
{
|
||||
if (player == null)
|
||||
{
|
||||
currentState = EnemyState.Patrolling;
|
||||
agent.isStopped = false;
|
||||
return;
|
||||
}
|
||||
|
||||
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
|
||||
|
||||
// Oto<74> se k hr<68><72>i
|
||||
Vector3 directionToPlayer = (player.position - transform.position).normalized;
|
||||
Vector3 move = new Vector3(directionToPlayer.x, 0, directionToPlayer.z);
|
||||
|
||||
Vector3 rayOrigin = transform.position + Vector3.down * detectionOffset;
|
||||
|
||||
bool isBlocked = Physics.Raycast(rayOrigin, move, obstacleDetectionDistance, obstacleMask);
|
||||
|
||||
if (isBlocked && isGrounded)
|
||||
{
|
||||
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
|
||||
}
|
||||
|
||||
charControl.Move(move * speed * Time.deltaTime);
|
||||
|
||||
velocity.y += gravity * Time.deltaTime;
|
||||
|
||||
charControl.Move(velocity * Time.deltaTime);
|
||||
|
||||
RotateTowardsPlayer(directionToPlayer);
|
||||
}
|
||||
|
||||
private void RotateTowardsPlayer(Vector3 directionToPlayer)
|
||||
{
|
||||
Quaternion targetRotation = Quaternion.LookRotation(new Vector3(directionToPlayer.x, 0, directionToPlayer.z));
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
|
||||
}
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 5f * Time.deltaTime);
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
if (player != null)
|
||||
// <20>tok na hr<68><72>e
|
||||
if (canAttack && distanceToPlayer <= attackRange)
|
||||
{
|
||||
Vector3 directionToPlayer = (player.position - transform.position).normalized;
|
||||
Vector3 moveDirection = new Vector3(directionToPlayer.x, 0, directionToPlayer.z);
|
||||
Vector3 rayOrigin = transform.position + Vector3.down * detectionOffset;
|
||||
Gizmos.color = Color.red;
|
||||
Gizmos.DrawRay(rayOrigin, moveDirection * obstacleDetectionDistance);
|
||||
StartCoroutine(Attack());
|
||||
}
|
||||
|
||||
// Pokud je hr<68><72> p<><70>li<6C> daleko, pokra<72>uj v pron<6F>sledov<6F>n<EFBFBD>
|
||||
if (distanceToPlayer > attackRange * 1.2f)
|
||||
{
|
||||
currentState = EnemyState.Chasing;
|
||||
agent.isStopped = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
private IEnumerator UpdatePath()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (currentState == EnemyState.Chasing && player != null)
|
||||
{
|
||||
agent.SetDestination(player.position);
|
||||
}
|
||||
else if (currentState == EnemyState.Patrolling)
|
||||
{
|
||||
agent.SetDestination(patrolTarget);
|
||||
}
|
||||
|
||||
yield return new WaitForSeconds(updatePathInterval);
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator CheckPlayerInRange()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (player != null && currentState != EnemyState.Attacking)
|
||||
{
|
||||
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
|
||||
|
||||
if (distanceToPlayer <= sightRange && distanceToPlayer > attackRange)
|
||||
{
|
||||
currentState = EnemyState.Chasing;
|
||||
}
|
||||
else if (distanceToPlayer <= attackRange)
|
||||
{
|
||||
currentState = EnemyState.Attacking;
|
||||
agent.isStopped = true;
|
||||
}
|
||||
}
|
||||
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator CheckCurrentRoom()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
// Zkontroluj, zda je enemy st<73>le v platn<74> m<>stnosti
|
||||
if (!agent.isOnNavMesh)
|
||||
{
|
||||
Debug.LogWarning("Enemy is off NavMesh, attempting to warp...");
|
||||
agent.Warp(transform.position);
|
||||
}
|
||||
|
||||
yield return new WaitForSeconds(roomCheckInterval);
|
||||
}
|
||||
}
|
||||
|
||||
private void GenerateNewPatrolTarget()
|
||||
{
|
||||
// Vyber n<>hodn<64> c<>l v okol<6F> v<>choz<6F> pozice
|
||||
Vector2 randomCircle = Random.insideUnitCircle * patrolRange;
|
||||
patrolTarget = patrolCenter + new Vector3(randomCircle.x, 0, randomCircle.y);
|
||||
|
||||
// Zajisti, <20>e c<>l je na NavMesh
|
||||
NavMeshHit hit;
|
||||
if (NavMesh.SamplePosition(patrolTarget, out hit, patrolRange, NavMesh.AllAreas))
|
||||
{
|
||||
patrolTarget = hit.position;
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator Attack()
|
||||
{
|
||||
canAttack = false;
|
||||
|
||||
// Spustit animaci <20>toku
|
||||
if (animator != null)
|
||||
{
|
||||
animator.SetTrigger("Attack");
|
||||
}
|
||||
|
||||
// Zde m<><6D>e<EFBFBD> p<>idat logiku po<70>kozen<65> hr<68><72>e
|
||||
Debug.Log("Enemy attacks player!");
|
||||
|
||||
yield return new WaitForSeconds(attackCooldown);
|
||||
canAttack = true;
|
||||
}
|
||||
|
||||
// Vol<6F>no p<>i smrti nep<65><70>tele
|
||||
public void Die()
|
||||
{
|
||||
StopAllCoroutines();
|
||||
|
||||
if (agent != null)
|
||||
{
|
||||
agent.isStopped = true;
|
||||
}
|
||||
|
||||
// Zde m<><6D>e<EFBFBD> p<>idat animaci smrti atd.
|
||||
Destroy(gameObject, 2f);
|
||||
}
|
||||
|
||||
// Pro vizualizaci v editoru
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
Gizmos.color = Color.red;
|
||||
Gizmos.DrawWireSphere(transform.position, attackRange);
|
||||
|
||||
Gizmos.color = Color.yellow;
|
||||
Gizmos.DrawWireSphere(transform.position, sightRange);
|
||||
|
||||
Gizmos.color = Color.blue;
|
||||
Gizmos.DrawWireSphere(patrolCenter, patrolRange);
|
||||
|
||||
if (Application.isPlaying)
|
||||
{
|
||||
Gizmos.color = Color.green;
|
||||
Gizmos.DrawSphere(patrolTarget, 0.3f);
|
||||
Gizmos.DrawLine(transform.position, patrolTarget);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user