2024-08-07 12:37:51 +01:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2024-08-12 23:52:18 +01:00
|
|
|
using System.Security.Cryptography;
|
2024-08-07 12:37:51 +01:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.AI;
|
|
|
|
|
|
|
|
//* General Comments or Finished Tasks
|
|
|
|
//TODO Tasks left to be done for game
|
|
|
|
//! Bugs or Issues
|
|
|
|
//? Questions or Suggestions
|
|
|
|
|
|
|
|
//TODO AI Script for the Stick Beating Game
|
|
|
|
//* AI for enemies to run away from the character once in range
|
|
|
|
//* AI should change direction every few seconds to make it harder for the player to hit them
|
2024-08-12 23:52:18 +01:00
|
|
|
//* AI should not go into a direction within the player's range
|
2024-08-07 12:37:51 +01:00
|
|
|
//* If player is not in range, AI should move randomly
|
2024-08-12 23:52:18 +01:00
|
|
|
//TODO Slow down AI when hit by player rock
|
|
|
|
//TODO AI dies if hit by player stick
|
|
|
|
//? Enemy AI gets stuck in corners - maybe have AI move in a random direction if it gets stuck
|
2024-08-07 12:37:51 +01:00
|
|
|
|
|
|
|
public class AIScript : MonoBehaviour
|
|
|
|
{
|
|
|
|
public NavMeshAgent agent;
|
2024-08-12 23:52:18 +01:00
|
|
|
public float range;
|
|
|
|
public Transform centrePoint;
|
|
|
|
private bool playerInRange = false;
|
|
|
|
public bool isHit = false;
|
|
|
|
private void Awake()
|
2024-08-07 12:37:51 +01:00
|
|
|
{
|
|
|
|
agent = GetComponent<NavMeshAgent>();
|
|
|
|
}
|
|
|
|
|
2024-08-12 23:52:18 +01:00
|
|
|
private void Update()
|
2024-08-07 12:37:51 +01:00
|
|
|
{
|
2024-08-12 23:52:18 +01:00
|
|
|
if(agent.remainingDistance <= agent.stoppingDistance && !playerInRange)
|
|
|
|
{
|
|
|
|
Vector3 point;
|
|
|
|
if(RandomPoint(centrePoint.position, range, out point))
|
|
|
|
{
|
|
|
|
Debug.DrawRay(point, Vector3.up, Color.blue, 1.0f);
|
|
|
|
agent.SetDestination(point);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(playerInRange && !isHit)
|
|
|
|
{
|
|
|
|
agent.speed = 7.0f;
|
|
|
|
RunAway();
|
|
|
|
}
|
2024-08-07 12:37:51 +01:00
|
|
|
|
2024-08-12 23:52:18 +01:00
|
|
|
if(isHit)
|
|
|
|
{
|
|
|
|
agent.speed = 1.75f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
agent.speed = 3.5f;
|
|
|
|
}
|
2024-08-07 12:37:51 +01:00
|
|
|
}
|
|
|
|
|
2024-08-12 23:52:18 +01:00
|
|
|
void RunAway()
|
2024-08-07 12:37:51 +01:00
|
|
|
{
|
2024-08-12 23:52:18 +01:00
|
|
|
Vector3 direction = transform.position - centrePoint.position;
|
|
|
|
Vector3 runTo = transform.position + direction.normalized;
|
|
|
|
NavMeshHit hit;
|
|
|
|
NavMesh.SamplePosition(runTo, out hit, 5.0f, NavMesh.AllAreas);
|
|
|
|
agent.SetDestination(hit.position);
|
2024-08-07 12:37:51 +01:00
|
|
|
}
|
|
|
|
|
2024-08-12 23:52:18 +01:00
|
|
|
bool RandomPoint(Vector3 center, float range, out Vector3 result)
|
2024-08-07 12:37:51 +01:00
|
|
|
{
|
2024-08-12 23:52:18 +01:00
|
|
|
Vector3 randompoint = center + Random.insideUnitSphere * range;
|
|
|
|
NavMeshHit hit;
|
|
|
|
if (NavMesh.SamplePosition(randompoint, out hit, 1.0f, NavMesh.AllAreas))
|
|
|
|
{
|
|
|
|
result = hit.position;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
result = Vector3.zero;
|
|
|
|
return false;
|
2024-08-07 12:37:51 +01:00
|
|
|
}
|
|
|
|
|
2024-08-12 23:52:18 +01:00
|
|
|
private void OnTriggerEnter(Collider other)
|
2024-08-07 12:37:51 +01:00
|
|
|
{
|
2024-08-12 23:52:18 +01:00
|
|
|
if(other.tag == "Player")
|
2024-08-07 12:37:51 +01:00
|
|
|
{
|
2024-08-12 23:52:18 +01:00
|
|
|
playerInRange = true;
|
2024-08-07 12:37:51 +01:00
|
|
|
}
|
|
|
|
}
|
2024-08-12 23:52:18 +01:00
|
|
|
private void OnTriggerExit(Collider other)
|
|
|
|
{
|
|
|
|
if(other.tag == "Player")
|
2024-08-07 12:37:51 +01:00
|
|
|
{
|
2024-08-12 23:52:18 +01:00
|
|
|
playerInRange = false;
|
2024-08-07 12:37:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|