Santi
d00afd6a48
randomly and runs away from player when close. Added NavMesh to scene. Updated documentation and tags.
114 lines
2.9 KiB
C#
114 lines
2.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
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
|
|
//TODO AI should not go into a direction within the player's range
|
|
//* If player is not in range, AI should move randomly
|
|
|
|
public class AIScript : MonoBehaviour
|
|
{
|
|
private bool runningCoroutine = false;
|
|
public GameObject player;
|
|
public NavMeshAgent agent;
|
|
|
|
//* Patroling
|
|
public Vector3 walkPoint;
|
|
public bool walkPointSet;
|
|
public float walkPointRange;
|
|
|
|
//* States
|
|
public bool playerInSightRange = false;
|
|
|
|
void Start()
|
|
{
|
|
|
|
player = GameObject.Find("Capsule");
|
|
agent = GetComponent<NavMeshAgent>();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if(!playerInSightRange) Patroling();
|
|
if(playerInSightRange) Running();
|
|
}
|
|
|
|
private void Patroling()
|
|
{
|
|
agent.speed = 3.5f;
|
|
if(!walkPointSet) SetWalkPointPatroling();
|
|
|
|
if(walkPointSet)
|
|
agent.SetDestination(walkPoint);
|
|
|
|
Vector3 distanceToWalkPoint = transform.position - walkPoint;
|
|
|
|
if(distanceToWalkPoint.magnitude < 1f)
|
|
walkPointSet = false;
|
|
}
|
|
|
|
private void SetWalkPointPatroling()
|
|
{
|
|
float randomZ = Random.Range(-walkPointRange, walkPointRange);
|
|
float randomX = Random.Range(-walkPointRange, walkPointRange);
|
|
|
|
walkPoint = new Vector3(randomX, transform.position.y, randomZ);
|
|
walkPointSet = true;
|
|
}
|
|
private void Running()
|
|
{
|
|
agent.speed = 6f;
|
|
if(!walkPointSet) SetWalkPointRunning();
|
|
|
|
if(walkPointSet)
|
|
agent.SetDestination(walkPoint);
|
|
if(!runningCoroutine) StartCoroutine(WaitTime());
|
|
|
|
Vector3 distanceToWalkPoint = transform.position - walkPoint;
|
|
|
|
if(distanceToWalkPoint.magnitude < 1f)
|
|
walkPointSet = false;
|
|
|
|
}
|
|
|
|
private void SetWalkPointRunning()
|
|
{
|
|
float randomZ = Random.Range(-walkPointRange, walkPointRange);
|
|
float randomX = Random.Range(-walkPointRange, walkPointRange);
|
|
|
|
walkPoint = new Vector3(randomX, transform.position.y, randomZ);
|
|
|
|
walkPointSet = true;
|
|
}
|
|
|
|
IEnumerator WaitTime()
|
|
{
|
|
runningCoroutine = true;
|
|
yield return new WaitForSeconds(Random.Range(2, 5));
|
|
walkPointSet = false;
|
|
runningCoroutine = false;
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other) {
|
|
if(other.gameObject == player)
|
|
{
|
|
playerInSightRange = true;
|
|
}
|
|
}
|
|
private void OnTriggerExit(Collider other) {
|
|
if(other.gameObject == player)
|
|
{
|
|
playerInSightRange = false;
|
|
}
|
|
}
|
|
}
|