- Home /
Question by
Trollvahkiin · Sep 22, 2013 at 02:00 PM ·
aichasemonster
How could I make the AI follow me after chase = true?we
Hey,
I got this script, it works fine and will fit my game but I need it to follow my character after chase is true.
var player : Transform; // the Object the player is controlling
var spawnOrgin : Vector3; // this will be the bottom right corner of a square we will use as the spawn area
var maximum : Vector3; // max distance in the x, y, and z direction the enemy can spawn
var spawnRate : float; // how often the enemy will respawn
var distanceToPlayer : float; // how close the enemy has to be to the player to play music
var chasing : boolean = false;
private var nearPlayer : boolean = false; // use this to stop the teleporting if near the player
private var nextTeleport : float = 0.0f; // will keep track of when we to teleport next
function Start ()
{
nextTeleport = spawnRate;
}
function Update ()
{
if (!nearPlayer) // only teleport if we are not close to the player
{
if (Time.time > nextTeleport) // only teleport if enough time has passed
{
transform.position = Vector3( Random.Range(spawnOrgin.x, maximum.x), Random.Range(spawnOrgin.y, maximum.y), Random.Range(spawnOrgin.z, maximum.z) ); // teleport
nextTeleport += spawnRate; // update the next time to teleport
}
}
if (Vector3.Distance(transform.position, player.position) <= distanceToPlayer)
{
if (audio && audio.clip && !audio.isPlaying) // play the audio if it isn't playing
audio.Play();
nearPlayer = true;
chasing = true;
}
else
{
if (audio)
audio.Stop();
nearPlayer = false;
}
if (chasing)
{
//this is where he would start the chase
}
}
Comment
Answer by ScotchBunny · Sep 23, 2013 at 03:10 PM
For a basic solution, equip your AI controlled character with a CharacterController and use the Move function. That will keep it from going through walls and objects. Have your AI check the position of the player and have it rotate towards it while moving forward. If you want your AI to step around obstacles you will have to look at some pathfinding.
Your answer
Follow this Question
Related Questions
How to have a cloned/duplicated enemy react the same way the original does? 1 Answer
Problem with chasing ai 1 Answer
AI Monster Problem 3 Answers
Monster AI 0 Answers