0
votes

i have added a animator to the enemy prefab and i want to make the animation change when i hit it in the collision ( its **** out ) please can someone help me:(

i tried to make a new var eanim : Animator.... and call it in the start ect... but wont let me drag the ememy animator into the slot

how can i fix this please.

outline what i want to do i want to coll with the enemy and have the enemy that i hit change to death animation.

var Player : GameObject;
var Gravity:float = 2;
var speed:float = 2;
var enemytrans : Transform;
var enemy: GameObject;
public var jumped = false;
var anim : Animator;

function Start () {
 while (true) {
     yield WaitForSeconds (Random.Range(3, 0));
     enemy = Instantiate(enemytrans).gameObject;
 }
anim = GetComponent(Animator);
}

function Update () {


Player.transform.position.x = -4.325;

if (jumped == false){
anim.SetFloat("hf",0.0);
}
if (Input.GetButtonDown("Fire1") && jumped==false){
fire();
jumped = true;
}
if(jumped==true){
anim.SetFloat("hf",1);
}
}
function OnCollisionEnter2D(coll: Collision2D) {
if(coll.gameObject.CompareTag("ground")){
anim.SetFloat("hf",0.0);
jumped=false;
}
*********if(coll.gameObject.CompareTag("enemy") && jumped==true){   **
fire(); 
jumped=true;
anim.SetTrigger("isdead");                  <<<<<<<<<<<<<----- this is what i need help with ------
}
if(coll.gameObject.CompareTag("enemy") && jumped==false){
Destroy(Player);
}
}


function fire(){
Player.transform.Translate(Vector3(Input.GetAxis("Vertical") * speed * Time.deltaTime, 0, 0));
Player.rigidbody2D.velocity = Vector2(0,10);
}
1

1 Answers

0
votes

If you do anim.SetTrigger(..) in unity, you also need to setup that inside the Animator window for the corresponding gameobject.

So select your gameobject and go to window -> animator, add a trigger parameter (in your case with the name of "isdead") - and setup the transitions from the different states. So for example I create an empty state and set that as default, and then drag between that and my animation state in order to get the transitions.

Inside the transitions you just set default -> anim state to use the "isdead" parameter under the conditions. And on anim state -> default you set exit time as the condition.