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
– RedDevil