Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

Play animation when hit by raycast

$
0
0
So i have my zombi walking to you but it doesnt react when you shot it, i have an animation for my zombie to make it jerk back when shot. But i dont know how to make the zombie play an animation when it comes in contact with the ray cast. Heres my current script var target : Transform; //the enemy's target var moveSpeed = 3; //move speed var rotationSpeed = 3; //speed of turning var myTransform : Transform; //current transform data of this enemy var isNotDead : boolean = true; var health : float = 100; function Awake() { myTransform = transform; //cache transform data for easy access/preformance } function Start() { target = GameObject.FindWithTag("Player").transform; //target the player } function Update () { if(health < 1){ isNotDead = false; animation.Play("fallToFace"); Destroy(gameObject, 1); } if(isNotDead){ //rotate to look at the player myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); var distance = Vector3.Distance(target.position, myTransform.position); if (distance < 3.0f) { animation.Play("attack1"); } else{ //move towards the player myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; animation.Play("shamble"); } } } function ApplyDamage(dmg : float){ health -= dmg; }

Viewing all articles
Browse latest Browse all 171066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>