Making enemy AI waypoints HELP!
Alright so i am making a little horror game and i want the enemy to patrol the area, around objects and up and down hills and stuff (my terrain isnt just flat). And when i go in the enemies line of sight or i get to close it comes after me and when i get to far it continues patrolling. I am using this code using UnityEngine; using System.Collections;
public class Chase : MonoBehaviour {
public Transform player;
public Transform head;
static Animator anim;
string state = "patrol";
public GameObject[] waypoints;
int currentWP = 0;
float rotSpeed = 0.2f;
float speed = 0.02f;
float accuracyWP = 5.0f;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
float angle = Vector3.Angle(direction, head.up);
if(state == "patrol" && waypoints.Length > 0)
{
anim.SetBool("isIdle", false);
anim.SetBool("isWalking", true);
if(Vector3.Distance(waypoints[currentWP].transform.position, transform.position) < accuracyWP)
{
currentWP++;
if(currentWP >= waypoints.Length)
{
currentWP = 0;
}
}
//rotate towards waypoint
direction = waypoints[currentWP].transform.position - transform.position;
this.transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.LookRotation(direction), rotSpeed * Time.deltaTime);
this.transform.Translate(0, 0, Time.deltaTime * speed);
}
if (Vector3.Distance(player.position, this.transform.position) < 20 && (angle < 30 || state == "pursuing"))
{
state = "pursuing";
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), rotSpeed * Time.deltaTime);
if (direction.magnitude > 5)
{
this.transform.Translate(0,0,Time.deltaTime * speed);
anim.SetBool("isWalking", true);
}
else
{
anim.SetBool("isWalking", false);
}
}
else
{
anim.SetBool("isIdle", true);
anim.SetBool("isWalking", false);
state = "patrol";
}
}
}
I got it off this video right hear. https://www.youtube.com/watch?v=OmoKw1ikAi8&t=777s The problem is that since my terrain isnt flat the enemy just floats a little bit above the ground and also i have 19 waypoints that zig zag in and out around the "town". So since the ground isnt flat and theirs obstacles the enemy either doesnt move or he floats around. I have tried adding a rigid body and checking gravity but all that does is makes the enemy fall over. So what i need is for someone to maybe edit this script for me so that the enemy ai will be able to go to all the waypoints, not float and stay on the ground, and when i get into the line of site he will stop patrolling and chase me and when i get to far he will stop and continue patrolling. I am not good at scripting so if you just type what i need without adding it to the whole script i wont know what to do Thanks!!
Answer by ericbegue · Dec 19, 2016 at 12:35 PM
You should not move your enemy by directly translating its position. Unity has a built-in navigation system, which essentially makes your AI aware of the environment and decide which path to take to move from point A to point B by taking into account possible ways and obstacles. It will simplifies your problem a lot if you make use of NavMesh and NavMeshAgent.
Your AI is based on a Finite State Machine (FSM). FSM is simple to understand and works great for simple AI. But it becomes unmanageable for more complex AIs. Behaviour Tree (BT) is a good tool for handling more complex AIs. Have a look at Panda BT (www.pandabehaviour.com), it's a scripting framework based on Behaviour Tree. The package contains several examples, include patrol-chase-attack behaviours. I'm the author. I'd be glad to help if you have any question about using this package or about Behaviour Tree in general. You're welcome on this forum thread.
Thanks so much i may need help with the BT but i will see what i can do with it first also what should i have in the script then or should i just delete that script and use th BT.
Defining a Behaviour Tree with Panda BT consists of 2 steps:
Define 'tasks' in a $$anonymous$$onoBehaviour.
Use these 'tasks' as building blocks to write a BT script.
Applying this to your problem, you would need first to define a task that makes the Nav$$anonymous$$esh agent move to some given destinations.
using UnityEngine;
using Panda; // We are using Panda BT
public class Nav$$anonymous$$eshBT : $$anonymous$$onoBehaviour
{
public Transform[] locations; // Some locations to move to
Nav$$anonymous$$eshAgent nav$$anonymous$$eshAgent;
void Start()
{
nav$$anonymous$$eshAgent = GetComponent<Nav$$anonymous$$eshAgent>();
}
[Task] // A task to move the Nav$$anonymous$$eshAgent toward a destination.
void $$anonymous$$oveTo(int locationIndex)
{
if (Task.current.isStarting)
{// When the task is starting, we set the destinaiton.
nav$$anonymous$$eshAgent.destination = locations[locationIndex].position;
}
else
if (nav$$anonymous$$eshAgent.remainingDistance <= nav$$anonymous$$eshAgent.stoppingDistance)
{// The task is done when we arrived at destination.
Task.current.Succeed();
}
}
}
Then you can use this task from a BT script to define how your AI behaves. For instance we want the AI to move back and forth between the first and the second location. Then, the BT script is:
tree("Root")
sequence
$$anonymous$$oveTo(0)
$$anonymous$$oveTo(1)
Don't hesitate to post on this forum thread if you have any question.
Should i delete the script that i have then and just use BT?
So, basically, you are asking me to write this script for you in exchange of credits on your youtube channel?...
What is your youtube channel?