Santi
b0be1fdd96
Player can throw rocks at enemy to slow them down. Enemy script split into two scripts, one for movement and one for detection. Added and edited some comments.
87 lines
2.9 KiB
C#
87 lines
2.9 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
|
|
//* 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
|
|
//! AI bugs out if player touches it and won't leave edge of map
|
|
|
|
public class AIScript : MonoBehaviour
|
|
{
|
|
public NavMeshAgent agent;
|
|
public float range;
|
|
public Transform centrePoint;
|
|
public bool isHit = false;
|
|
public detectionScript detectionScript;
|
|
private void Awake()
|
|
{
|
|
//* Gets compomnents at start
|
|
agent = GetComponent<NavMeshAgent>();
|
|
detectionScript = GetComponentInChildren<detectionScript>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
//* If enemy is within stopping distance and player is not in range, move to a random point in range
|
|
if(agent.remainingDistance <= agent.stoppingDistance && !detectionScript.playerInRange)
|
|
{
|
|
agent.speed = 3.5f;
|
|
Vector3 point;
|
|
if(RandomPoint(centrePoint.position, range, out point))
|
|
{
|
|
Debug.DrawRay(point, Vector3.up, Color.blue, 1.0f); //* Displays a blue ray to show where the enemy is moving
|
|
agent.SetDestination(point); //* Sets the destination of the enemy to the random point
|
|
}
|
|
}
|
|
else if(detectionScript.playerInRange && !isHit) //* If player is in range, run away
|
|
{
|
|
agent.speed = 7.0f;
|
|
RunAway();
|
|
}
|
|
|
|
if(isHit)
|
|
{
|
|
agent.speed = 1.75f;
|
|
}
|
|
}
|
|
|
|
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))
|
|
{
|
|
Vector3 point = hit.position;
|
|
Debug.DrawRay(point, Vector3.up, Color.blue, 1.0f);
|
|
result = hit.position;
|
|
return true;
|
|
}
|
|
result = Vector3.zero;
|
|
return false;
|
|
}
|
|
|
|
|
|
}
|