- Home /
Question by
JohnoONE · Jul 13, 2016 at 07:04 PM ·
animationjavascriptcollider
Playing Different Animation Troubles
I want my code to have my gameobject (Goblin) to stand animation when player is far away, then when player is close to chase and use the walk animation. Then when player is super close to play the attack01 animation, Finally I want the enemy to die when its hitpoints = 0 and play dead animation.
My problem is the attack01 animation doesnt stop playing and the dead animation wont start.
Here is my code; any help would be appreciated
#pragma strict
var maxHealth = 10;
static var EXPgive = 50;
static var _isdead = false;
var IgnoreDistance = 12.0f;
var AttackDistance = 4.0f;
var rotationSpeed = 1.2f;
var moveSpeed = 1.2;
var target : Transform;
var myTransform : Transform;
function Awake () {
myTransform = transform;
maxHealth = 10;
EXPgive = 50;
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
myTransform.position.y = 0.4;
var distance = Vector3.Distance(myTransform.position, target.position);
if (distance >= IgnoreDistance){
WaitforPlayer();
}
if (distance <= IgnoreDistance){
lookforPlayer();
}
if (distance <= IgnoreDistance){
if (distance >= AttackDistance){
moveAtPlayer();
}
}
}
function moveAtPlayer(){
transform.LookAt(target);
animation.CrossFade("walk");
transform.position += transform.forward*moveSpeed*Time.deltaTime;
}
function WaitforPlayer(){
animation.CrossFade("stand");
}
function lookforPlayer(){
animation.CrossFade("stand");
transform.LookAt(target);
}
function ApplyDamage(damage : float){
if(maxHealth <= 0.0){
return;
}
maxHealth -= damage;
_isdead = true;
animation.CrossFade("damage");
transform.LookAt(target);
if(maxHealth <= 0.0){
Die();
}
}
function Die(){
Player.enemydied = true;
Player.curEXP+=50;
Debug.Log("stop animation here");
animation.Stop("attack01");
animation.CrossFade("dead");
yield WaitForSeconds(2);
Destroy(this.gameObject);
Player.enemydied = false;
}
function OnTriggerStay (other : Collider) {
if(other.transform.tag == "Player"){
animation.CrossFade("attack01");
Debug.Log("E took a swing");
}
}
function OnTriggerExit (other : Collider){
if(other.gameObject.tag == "Player") {
Debug.Log("E Left");
}
}
Comment
Your answer
Follow this Question
Related Questions
How To Play Animation 1 Answer
Raycasting fail 1 Answer
Animation Script not working. 1 Answer
Unity 4.5.2 bug changing the properties of the 2d colliders in animation 1 Answer