- Home /
AI enemy scripting help
I need someone to please help me script an enemy AI that will attack my player and return to spawn if hes too far away. I also need this script to be able to play an attack animation an idle animation running and dieing animation. If someone could help me it would be greatly appreciated.
Answer by FLASHDENMARK · Feb 17, 2011 at 02:14 PM
This will get you going:
var distance; var target : Transform; var lookAtDistance = 15.0; var attackRange = 10.0; var moveSpeed = 5.0; var damping = 6.0; private var isItAttacking = false;
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
if(distance < lookAtDistance)
{
isItAttacking = false;
renderer.material.color = Color.yellow;
lookAt ();
}
if(distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if(distance < attackRange)
{
attack ();
}
if(isItAttacking)
{
renderer.material.color = Color.red;
}
}
function lookAt () { var rotation = Quaternion.LookRotation(target.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping); }
function attack () { isItAttacking = true; renderer.material.color = Color.red;
transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
}
You can always implement other thins like animations and stuff. This simple AI will chase you when you come close enough, until you run away again. Just drag the target you want the AI to follow in the Inspector.
I can give you a little help with the animations. You have to make them your selv, and if you want the AI to do a animation when a certan condition is true.
if(Something happened) // If some event
{
animation.Play("The animations name"); // Then play the animation.
}
If you still got any questions I can try to explain it more in depth a little later, I have to go to school in a minut ;).
Thank you for your help! I <3 U LOL jkjk can you also show me how to incorporate the animations?
I have explained it a little in the edited answer. If you are still in doubt than just ask, and i will try to explain it a little more in depth later, but for now I have to go to school.(9 th grade is a *itch) ;)
Oh an BTW i <3 you to ;)
I have one Question - How to prevent enemy going through walls? Seems like colliders are not affecting it's behaviour..
@ashanimation Yes, that is very true. It will go through walls unfortunately. But you could use "Raycasting" when it hits a object change the position of the AI(make it go sideways or something). Check this link for raycasting:http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html Happy to help. :)
how can I put the collision on this script, I have a pacman type game... the object simply pass thru walls :P
Your answer
Follow this Question
Related Questions
Shooting Damage Help 1 Answer
Stay Back! 2 Answers
How to stop enemy shooting through wall 1 Answer
Enemy AI With changing Player 0 Answers
How do you move did with [iTWEEN] the enemy has a component of [Character Controller]? 0 Answers