Mini-Games-Game/Assets/Scripts/StickBeatings/AIScript.cs
Santi 857973aff7 Beating-With-Sticks: Improved AI "patrolling" state and AI runs away
from player once in certain distance.
Made a second Movement script which allows the player to throw rocks.
Added/changed some comments for suggestions, things to do, things that
are done and bugs that need fixing.
2024-08-12 23:52:18 +01:00

97 lines
2.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
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
//* AI should not go into a direction within the player's range
//* If player is not in range, AI should move randomly
//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
public class AIScript : MonoBehaviour
{
public NavMeshAgent agent;
public float range;
public Transform centrePoint;
private bool playerInRange = false;
public bool isHit = false;
private void Awake()
{
agent = GetComponent<NavMeshAgent>();
}
private void Update()
{
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();
}
if(isHit)
{
agent.speed = 1.75f;
}
else
{
agent.speed = 3.5f;
}
}
void RunAway()
{
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);
}
bool RandomPoint(Vector3 center, float range, out Vector3 result)
{
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;
}
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
playerInRange = true;
}
}
private void OnTriggerExit(Collider other)
{
if(other.tag == "Player")
{
playerInRange = false;
}
}
}