- Home /
The question is answered, right answer was accepted
Unity3D AI using animations
Hi!I have been learning Unity3D for the past weeks and i stumbled upon a problem. I dont know how to properly make the enemy play the animations. Here is the code i have up to now:
var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var attackRange = 2.0;
var MoveSpeed = 5.0;
var Damping = 6.0;
var attackRepeatTime = 2.0;
private var attackTime : float;
var chaseRange = 15.0;
var controller : CharacterController;
var gravity : float = 20.0;
var TheDamage = 40.0;
private var MoveDirection : Vector3 = Vector3.zero;
//anim vars
var AttackAnim : Transform;
var WalkAnim : Transform;
var RunAnim : Transform;
function Start()
{
attackTime = Time.time;
}
function Update()
{
if(RespawnMenuV2.playerIsDead == false)
{
Distance = Vector3.Distance(Target.position,transform.position);
if(Distance < lookAtDistance)
{
lookAt();
}
if(Distance < attackRange)
{
attack();
}
else if(Distance < chaseRange)
{
chase();
WalkAnim.animation.CrossFade("Walk");
}
}
}
function lookAt()
{
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation , rotation, Time.deltaTime * Damping);
}
function chase()
{
WalkAnim.animation.Play("Walk");
MoveDirection = transform.forward;
MoveDirection *= MoveSpeed;
MoveDirection.y-=gravity * Time.deltaTime;
controller.Move(MoveDirection * Time.deltaTime);
}
function attack()
{
if(Time.time > attackTime)
{
AttackAnim.animation.Play("Attack");
Target.SendMessage("ApplyDamage",TheDamage,SendMessageOptions.DontRequireReceiver);
Debug.Log("TheEnemyHasAttacked!");
attackTime = Time.time + attackRepeatTime;
AttackAnim.animation.CrossFade("Attack");
}
}
function ApplyDamage()
{
RunAnim.animation.Play("Run");
chaseRange += 30;
MoveSpeed += 2;
lookAtDistance += 40;
RunAnim.animation.CrossFade("Run");
}
So I have 3 animations that i want to attach to the enemy AI:Walk,Run,Attack. I want that when I get close to the enemy he walks up to me and starts atacking.If i hit the enemy( ApplyDamage()) then i want him to use the run animation to chase me. My Unity3D knowledge is not very large so if you guys chould help me I would really apreciate it.I tryed using var AttackAnim : AnimationClip but i never worked with those so I dont know how it would be done.Thank you
var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var attackRange = 2.0;
var $$anonymous$$oveSpeed = 3.0;
var Damping = 6.0;
var attackRepeatTime = 2.0;
private var attackTime : float;
var chaseRange = 15.0;
var controller : CharacterController;
var gravity : float = 20.0;
var TheDamage = 40.0;
private var $$anonymous$$oveDirection : Vector3 = Vector3.zero;
//anim vars
var AttackAnim : AnimationClip;
var WalkAnim : AnimationClip;
var RunAnim : AnimationClip;
function Start()
{
attackTime = Time.time;
}
function Update()
{
if(Respawn$$anonymous$$enuV2.playerIsDead == false)
{
Distance = Vector3.Distance(Target.position,transform.position);
if(Distance < lookAtDistance)
{
lookAt();
}
if(Distance < attackRange)
{
attack();
}
else if(Distance < chaseRange)
{
chase();
animation.CrossFade(WalkAnim.Walk);
}
}
}
function lookAt()
{
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation , rotation, Time.deltaTime * Damping);
}
function chase()
{
animation.Play(WalkAnim.Walk);
$$anonymous$$oveDirection = transform.forward;
$$anonymous$$oveDirection *= $$anonymous$$oveSpeed;
$$anonymous$$oveDirection.y-=gravity * Time.deltaTime;
controller.$$anonymous$$ove($$anonymous$$oveDirection * Time.deltaTime);
}
function attack()
{
if(Time.time > attackTime)
{
animation.Play(AttackAnim.Attack);
Target.Send$$anonymous$$essage("ApplyDamage",TheDamage,Send$$anonymous$$essageOptions.DontRequireReceiver);
Debug.Log("TheEnemyHasAttacked!");
attackTime = Time.time + attackRepeatTime;
animation.CrossFade(AttackAnim.name);
}
}
function ApplyDamage()
{
animation.Play(RunAnim.Run);
chaseRange += 30;
$$anonymous$$oveSpeed += 2;
lookAtDistance += 40;
animation.CrossFade(RunAnim.Run);
Debug.Log("Applyed D$$anonymous$$G is ON!");
}
These are the modifications to the code after your first comment.
forgot to say but the issue with the weapon switching and animation not playing! when im starting the game i get imediately :Animator has not been initialized. UnityEngine.Animator:SetBool(String, Boolean) WeaponSwitching:SelectWeapon(Int32) (at Assets/Scripts/WeaponSwitching.js:80) WeaponSwitching:Awake() (at Assets/Scripts/WeaponSwitching.js:9)
I solved this problem by enabling the "standard weapon".It was left to disabled
Answer by tanoshimi · Nov 19, 2013 at 11:43 AM
var AttackAnim : Transform;
var WalkAnim : Transform;
var RunAnim : Transform;
need to be:
var AttackAnim : AnimationClip;
var WalkAnim : AnimationClip;
var RunAnim : AnimationClip;
Then drag your three animation clips onto those three slots in the inspector and give it another go. There may well be other issues, but fix that first and let us know how you get on.
Also, try replacing all references like this:
RunAnim.animation.Play("Run");
AttackAnim.animation.CrossFade("Attack");
with references like this:
animation.Play(RunAnim.name);
animation.CrossFade(AttackAnim.name);
Thank you for the prompt response. I am having the following isues: First the enemy comes to me like sliding and is not really playing the walk animation! 2.He does keep playing the atack animation like in a loop when he is close to me.I only want this to happend every time he atacks.And that is set to 2 seconds.So I want him to atack then stop the animation and 2 seconds later do it again. 3.Run animation does not apear to work either and he seems to only loop atack animation while following me at a grater speed. Another isue unregarded to this is that i have multiple weapons and when im starting the game im starting with a weapon and is not detecting it as a weapon(i have fists as weapon too) and plays the animations of fists.It only plays the right animation if i change weapon.Is there a way to make the weapon that im starting the game with to be my fists? Also another question is how chould i atach a sound to the animations? Thank you very much!
forgot to say but the issue with the weapon switching and animation not playing! when im starting the game i get imediately :Animator has not been initialized. UnityEngine.Animator:SetBool(String, Boolean) WeaponSwitching:SelectWeapon(Int32) (at Assets/Scripts/WeaponSwitching.js:80) WeaponSwitching:Awake() (at Assets/Scripts/WeaponSwitching.js:9)
after further testing i found that the walk animation and run animation do not play because of this error: The animation state could not be played because it couldn't be found! Please attach an animation clip with the name '' or call this function only for existing animations.
Is that after you replaced all instances of the code snippets above like suggested?
Follow this Question
Related Questions
Horror Game AI script recommendation? 1 Answer
Problems with simple AI script 3 Answers
How to make a Script So that my Player Run As Soon as One Hits Play 0 Answers
Animation & Script Help 2 Answers
Enemy Animation Freezing in first frame 2 Answers