- Home /
Creating basic enemy animation and attacking
Hey Guys, I am trying to create a game for my class and I am having trouble with the enemy ai and animation. I have this model I found online, and I am trying to get it to attack the First Person player when it gets close to it, and to have it attack them and damage them. I also want to add animation so that when its invisible health-bar is depleted, it will act and it will fall down. Can any one tell me what I need to do in order to get it to do that? I don't have any other models of it expect its idle pose?
Answer by psychentist · Apr 20, 2011 at 02:44 AM
okay, first of all, you need to get Blender. It's a free 3d modeling application with the ability to rig and animate your models, and it runs on just about anything. (windows, mac, linux) there are tons of tutorials on youtube on how to rig and animate.
next, about attacking.
lets use a zombie as an example.
so I make my zombie and give him a walk and attack animation. as far as A.I., the only thing he will do is walk up and attack. (he's a zombie, that's all he needs, right?)
ok so I import my zombie into unity. now copy the "smooth follow" script and in the copy, disable the Z axis of rotation, and attach the script to the zombie. he will now rotate accordingly to always "watch" the player. now I need to make 3 colliders. the first will be a rather large sphere collider. this will be the aggro detector. we tell it that whenever the player enters the aggro collider, attack him. if I remember correctly, each object can only have one collider, so each of these colliders will have to be attached to a child object. code for the aggro collider is as follows. (I've been out of the game for over a year now, so if this code needs tweaking, cut me some slack, okay?)
var zombiesSpeed : float = 2.5;
// we can adjust his speed later to fit the game's needs.
var isChasing : boolean = false;
animation["Walk"].wrapMode = WrapMode.Loop; function OnCollisionEnter(){ isChasing = true; animation.play ("Walk"); }
function OnCollisionExit(){ isChasing = false; animation.stop; }
function Update(){ if isChasing = true { transform.translate (zombiesSpeed,0,0); } }
okay, now the zombie will move forward whenever he is inside the aggro collider.(he's already rotating to face him, remember.) now we need to tell the zombie to attack whenever he is in range. that's where our next collider coms into play. this one is a box collider that just sits in front of the zombie, and is only big enough to encompass the reach of his arm. the principle is the same.
var isAttacking : boolean = false; animation["Attack"].wrapMode = WrapMode.Loop; function OnCollisionEnter (){ isAttacking = true; animation.play ("Attack"); }
function OnCollisionExit (){ isAttacking = false; animation.stop; }
so now the zombie will swing his arm at us, but at this point no ill effect will come of it. this is where our last collider comes into play. when you "rig" your model, you will essentially build a framework of "bones" for your mesh to follow. the last collider is a small sphere collider, which is made to be a child object of the zombies hand bone. now the code for that:
function OnCollisionEnter (other : collider){
if (other.tag == "Player" && attackCollider.isAttacking == true){
playerStatus.Life -= 1;
}
}
Okay, whew. now the player will take damage if he touches the zombie's hand, but only if the zombie is attacking. the line about playerStatus.Life -=1 is referring to another script called Player Status. this script needs a variable called Life. (duh) it can be applied to the player himself, or just an empty game object. it will need at least the following line of code
static var Life : integer = 5;
hope this helps, and good luck on your project.
Answer by Muzz 1 · Apr 16, 2011 at 09:07 PM
What you need to do: 1. You need to animate it. If you're asking such questions, then it may be better to get animated characters. 2. Next, you may want to write a cross-fade animation script. Check out the FPS tutorial - it's got a good example. 3. Now you need to do an AI script. Again, the FPS tutorial is your friend.
Just consider (no offence meant) -- Are you being too ambitious? Why not try and make something slightly simpler first?
Answer by Alex 21 · Apr 17, 2011 at 12:28 AM
You need a 3D modeler like Cheetah3d. You can import your model into cheat and create animations, and then, once created bring it back to unity.
You then call a script to play the animation when shooting.
Answer by Brad Mance · Apr 20, 2011 at 01:21 AM
I would try to make something simpler, but it is required though. Also, I am running vista on a non-mac labtop so cheetah3d is out of the question. I have an idea though. Could I possibly create a script to have particles shooting at my character when they are on view? If so, could you try to point me in the right direction.