- Home /
Need help on modifying Enemy AI script.
I have this script gotten from a tutorial that makes enemy wander around the terrain which is perfect for the project I'm working on. I want to ask is there is a way for the wandering AI to detect and follow when a player gets too close? I'm new to javascript and trying to learn as much as I can if there is a good tutorial please point me that way. Thank you very much.
var target : Transform; var moveSpeed = 0.05; var rotationSpeed = 5; var myTransform : Transform;
function Awake() { myTransform = transform; }
function Start() {
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
Getting the distance isn't enough, you should also consider the field of view. Here's a nice article that explains that (generally), along with some other Linear Algebra game-related stuff.
You'd probably want your enemy to have different states, Idle, Haunting, Attacking, etc. - Here's a nice tutorial on state machines.
Answer by MFen · Oct 11, 2013 at 03:59 AM
I would create a script on each enemy and use the following in the update.
float distance = Vector3.Distance(transform.Position, player.transform.Position);
if (distance < 10)
Debug.Log("Attack!!!");
Your answer
Follow this Question
Related Questions
losing lives 1 Answer
Slerp Problem?: Enemy in Constant Rotation! 1 Answer
Finding Positions of other game objects and using them as an argument for an if statement. 0 Answers
[SOLVED] Enemy Script : Mob doesn't take damage 2 Answers
Apply Damage To Player On Collison With Specific Game Object 1 Answer