- Home /
Enemy AI problems
Well I got my slime enemy moving toward the character but the problem is that he keeps moving. I want to make it so that when he enters a certain distance from the character he will stop moving and start attacking the character. script is:
var attackanimation: AnimationClip;
var walkanimation: AnimationClip;
var dieanimation: AnimationClip;
private var walkspeed: float = -3.0;
private var stop: float = 0;
var range : float = .5;
var character : CharacterController;
var target : CharacterController;
var direction : float = -1;
function Update () {
distance = Vector3.Distance(transform.position, target.transform.position);
if(distance >= 1)
{
character.Move(Vector3.forward * walkspeed * direction * Time.deltaTime);
animation.Play(walkanimation.name);
}
if(distance <= 1){
animation.Play(attackanimation.name);
character.Move(Vector3.forward * stop * direction * Time.deltaTime) ;
}
}
Answer by timsk · Oct 24, 2011 at 10:46 AM
You nearly have it.
The main problem i can see is in this bit:
if(distance <= 1){
animation.Play(attackanimation.name);
character.Move(Vector3.forward * stop * direction * Time.deltaTime) ;
}
Just for starters, a distance of 1 is tiny, i'm not sure of the scale your using in your game but you might want something slightly bigger.
Now, we know we do not want to move the enemy if its in range, so lets delete the character.Move bit and just play the animation, then run the attack function:
var attackanimation: AnimationClip;
var walkanimation: AnimationClip;
var dieanimation: AnimationClip;
private var walkspeed: float = 3.0; //Don't understand why this was negative.
var range : float = .5;
var character : CharacterController;
var target : Transform; // lets cache the transform, rather than use a lookup.
var direction : float = -1; //why is this here?
function Awake()
{
target = GameObject.Find("your target").GetComponent(Transform);
}
function Update () {
distance = Vector3.Distance(transform.position, target); //no more lookup for target.
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
if(distance >= range)//you have a range variable, lets use it!
{
character.SimpleMove(forward * speed); //SimpleMove is simpler :).
animation.CrossFade(walkanimation.name); //let's make the anims look better.
}
if(distance <= range)//you have a range variable, lets use it!
{
animation.CrossFade(attackanimation.name); //crossfading again.
DamageorAttackFunction();
}
I hope the comments in the code help you to understand it. Might be 1 or 2 errors as i haven't tested it but nothing you shouldn't be able to solve.
Let me know how it goes!
distance = Vector3.Distance(transform.position, target); this dosnt work
Answer by Tracey P · Nov 14, 2011 at 04:02 AM
Ok hey guys I got him working with the help of Timsk, but now Im trying to figure out how to make him rotate around and chase me after I dodge him. I cant seem to figure it out. Im looking through the scripting guide to find it, but I could use some help. Thanks for the Help. Heres the script
var attackanimation: AnimationClip;
var walkanimation: AnimationClip;
var dieanimation: AnimationClip;
var distance;
private var walkspeed: float = -3.0; //Don't understand why this was negative.
var range : float = .5;
var character : CharacterController;
var target : Transform; // lets cache the transform, rather than use a lookup.
var direction : float = -1; //why is this here?
function Awake()
{
target = GameObject.Find("your target").GetComponent(Transform);
}
function Update () {
distance = Vector3.Distance(transform.position, target.transform.position);
}
function LateUpdate () {
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
if(distance >= range)//you have a range variable, lets use it!
{
character.SimpleMove(forward * walkspeed); //SimpleMove is simpler :).
animation.CrossFade(walkanimation.name); //let's make the anims look better.
}
if(distance <= range)//you have a range variable, lets use it!
{
animation.CrossFade(attackanimation.name);
}
}
target.transform.position is redundant, target is a transform, so just use target.position
you have posted this as an answer to your own question, however, it is in fact a new question and should have been posted as such.
From my understanding you want the character to look at the player. For this you might want to look into Transform.LookAt
Hey thanks and I know I just wanted to make it as easy as possible for those who wish to view this after it has worked for me
Your answer
Follow this Question
Related Questions
Different enemy types script design 1 Answer
Problem with enemy AI 1 Answer
How do I make my AI attack in the FPS tutorial? 0 Answers
Melee range AI 1 Answer