- Home /
Enemy AI monster problem
hi, I dont speak english really well but i can understand. I have a javascript code to make follow an object to another:
var leader : Transform;
var follower : Transform;
var speed : float =5; // The speed of the follower
function Update(){
follower.LookAt(leader);
follower.Translate(speed*Vector3.forward*Time.deltaTime);
animation.Play("Walk");
}
I use it for make an enemy but there is 2 problems : My enemy walk trough walls and can follow me from any distance, i want to controll it, please, can you help me ? [sry bad english] Bye :)
Answer by Peter G · Mar 04, 2012 at 03:58 PM
You don't want to use Transform.Translate() because that overrides the collision engine. It doesn't take much work to fix fortunately. Just add a character controller to your enemy. Then your code doesn't need many changes.
var leader : Transform;
var follower : Transform;
var followerCC : CharacterController;
var speed : float =5; // The speed of the follower
function Start () {
followerCC = follower.GetComponent.<CharacterController>();
}
function Update(){
follower.LookAt(leader);
followerCC.SimpleMove( follower.forward * speed );
//See character controller.SimpleMove(). Moves the object in that direction.
//automatically multiplies by Time.deltaTime;
//And responds to collisions
//ignores vertical component of vector and uses built-in gravity instead.
animation.Play("Walk");
}
I assume your code is attached to the monster so you don't have to cache the follower transform, but it doesn't hurt. In fact it will probably speed your code up some.
And you will have to explain to me the latter part of your question. I take it as you don't want the enemy following you forever i.e. it has a zone that it stays in. In that case, just create a large trigger around the area the enemy is allowed in. When the player enters the trigger, attack, and when he/she leaves, stop.
Answer by aldonaletto · Mar 04, 2012 at 04:01 PM
Translate doesn't check collisions - that's why the enemy pass through things. Usually we add a CharacterController to the enemy (menu Component/Physics/CharacterController), and move it using Move or SimpleMove. You should also check the distance from the target (leader, in your case), and stop following it when it's greater than the detect range:
var leader : Transform; var follower : Transform; var detectRange = 30.0; var speed : float = 5; // The speed of the follower
function Update(){ var dist = Vector3.Distance(leader.position, follower.position); if (dist
var leader : Transform; var follower : Transform; var detectRange = 30.0; var speed : float = 5; // The speed of the follower private var character: CharacterController;
function Update(){ // save the CharacterController in a variable to improve performance: if (!character) character = GetComponent(CharacterController); var dir = leader.position - follower.position; // find the leader direction dir.y = 0; // keep the direction in the horizontal plane if (dir.magnitude
@aldonaletto There is error in your 2 scripts, it says : BCE0043 unexepted token: 30. could you fix it please, I need to choose the distance. thank you.
That was an asha$$anonymous$$g syntax error! It should be:
var detectRange : float = 30.0;
or simply
var detectRange = 30.0;
I'm editing the answer, but UA is lasting an unpredictable amount of time to update things.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064742im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Bug with my enemyAI 2 Answers
Monster AI patrol an idle 0 Answers
Why does this script not work? 2 Answers
AI Airplane Problem 0 Answers
NavMesh AI: follow only if seen 1 Answer