- Home /
Enemy AI move random.
Im looking for a way to make the enemies on my map move randomly around if the player isnt in distance.. heres my AI script. Please and thank you :]
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
private Transform myTransform;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
}
// Update is called once per frame
void Update () {
float distance = Vector3.Distance(target.transform.position, transform.position);
Debug.Log(distance);
if(distance < 10 && distance > 2) {
Debug.DrawLine(target.position, myTransform.position, Color.red);
//look at target/rotate
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
//move towards target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
animation.Play("walk");
}
if(distance < 2) {
animation.Play("attack");
}
if(distance > 10) {
animation.Play("idle");
}
}
}
Also, you really should accept correct answers, and upvote them if they were helpful. It's rude not too, and if you don't, the whole unity answers system starts to break down. :/
If you're new to the site, watch this.
Answer by Meater6 · Dec 27, 2013 at 08:44 PM
Hello convictcartel!
I'm not that good in C#, so I can't write you a proper script for it to work, but I do know how to do this.
First off, you should be familiar with the Random class.
I'm unsure whether you want to teleport your enemies to random locations, or you simply want them to move to a random point nearby.
The later is fairly simple with your AI setup, and I assume that is what you want. Have a new variable called targetPos (or etc.), and make it normally equal to "target.position". When the player is far enough from the enemy, set targetPos like so:
//Note: this makes them move in a random SQUARE, not a circle. //A circle would require an angular-radius system.
public float width = 5; //Your square width
float rx = Random.Range(-1,1);
float rz = Random.Range(-1,1);
targetPos = Vector3(transform.position.x+(rx*width), someYValue, transform.position.z+(rx*width));
This code is untested. Not only that, I'm not great in C#. Have your enemy move to targetPos instead of target.position.
If the enemy will always be far out of view when moving randomly (and traveling doesn't matter), it will be much more optimal to teleport it. Doing so will vary greatly on the type of game you are making, so I will not post it here.
I bid you good luck, and I hope this helps!
Your answer
![](https://koobas.hobune.stream/wayback/20220613124958im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Random Running and Follow at Certain Distance 0 Answers
Make AI walk around randomly until Player is Seen. (C#) 2 Answers
Random Movement Problem 1 Answer
AI pathfinding waypoints 1 Answer
random.range not working 1 Answer