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

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

1 Answer

1

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);

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?

yes i did all that you suggested and the only animation that is playing is animation.CrossFade(AttackAnim.Attack); and its playing in a loop