- Home /
Stop an animation and play another
Well ive been having this issue for a while now and cant get it to work I have an enemy and when the player touches him he dies I just cant get his dying animation to complete it just finishes half way and the enemy continues his previous walking animation here is the code:
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var attackThreshold = 1.5; // distance within which to attack
var chaseThreshold:float = 10; // distance within which to start chasing
var giveUpThreshold = 20; // distance beyond which AI gives up
attackRepeatTime = 1; // delay between attacks when within range
private var chasing = false;
private var attackTime: float;
var myTransform : Transform; //current transform data of this enemy
function Awake() {
myTransform = transform; //cache transform data for easy access/preformance
}
function Start() {
target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () {
var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition); // check distance to target every frame:
var distance = (target.position - myTransform.position).magnitude;
if (chasing) {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
// give up, if too far away from target and destroy the game object
if (distance > giveUpThreshold) {
chasing = false;
Destroy(gameObject);
}
// attack, if close enough, and if time is OK:
if (distance < attackThreshold && Time.time > attackRepeatTime) {
moveSpeed=0;
animation.Play("Gimp Attack 1");
attackTime = Time.time+ attackRepeatTime;
}
}
else {
// not currently chasing.
// start chasing if target comes close enough
if (distance < chaseThreshold) {
chasing=true;
animation.Play("Gimp Up 1"); animation["Gimp Run 1"].layer=85; animation.Play("Gimp Run 1"); animation["Gimp Run 1"].weight=0.4f;
//Play an animation here
}
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
if (Physics.Raycast (ray, hit, 100))
{
if(hit.collider.gameObject.tag == "Enemy 1")
{
chaseThreshold =0;
moveSpeed = 0;
attackThreshold = 0;
rotationSpeed = 0;
transform.LookAt(target);
animation.Play("Gimp Death 1");
}
}
}
}
my answer did not show up yet so ill just post it as a comment i attached the code as a notepad, so please if you can tell me what i did wrong and why isnt the death animation playing when i touch the enemy on my device while other anims work fine in the same script.Thanks
[link text][1]
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Change the first frame of an animation 1 Answer
How do I use multiple animations at once. 1 Answer
Question, Need Help! 1 Answer