Question by
Raviraja190 · Jun 28, 2016 at 08:44 PM ·
health-deductionhealth
Continues Health Decrease problem after no collision
using UnityEngine; using System.Collections;
public class enemyDamage : MonoBehaviour {
public float Damage;
public float DamageRate;
public float PuskBackForce;
float nextDamage;
bool PlayerInRange = false;
GameObject thePlayer;
PlayerHealth thePlayerHealth;
void Start () {
nextDamage = Time.time;
thePlayer = GameObject.FindGameObjectWithTag ("Player");
thePlayerHealth = thePlayer.GetComponent<PlayerHealth>();
}
// Update is called once per frame
void Update () {
if(PlayerInRange) Attack ();
}
void OnTriggerEnter (Collider other){
if (other.tag == "Player") {
PlayerInRange = true;
}
}
void OnTriggerExit (Collider other) {
if (other.tag == "PLayer"){
PlayerInRange = false;
}
}
void Attack(){
if (nextDamage <= Time.time ){
thePlayerHealth.addDamage(Damage);
nextDamage = Time.time + DamageRate;
PushBack (thePlayer.transform);
}
}
void PushBack (Transform pushedobject){
Vector3 pushDirection = new Vector3 (0, (pushedobject.position.y - transform.position.y), 0).normalized;
pushDirection *= PuskBackForce;
Rigidbody pushedRB = pushedobject.GetComponent <Rigidbody> ();
pushedRB.velocity = Vector3.zero;
pushedRB.AddForce (pushDirection, ForceMode.Impulse);
} }
Comment
Best Answer
Answer by btmedia14 · Jun 28, 2016 at 09:23 PM
In function OnTriggerExit, change the value of tag being checked. Looks like a typo with a capital "L":
if (other.tag == "PLayer"){
PlayerInRange = false;
}
should probably be:
if (other.tag == "Player"){
PlayerInRange = false;
}
Your answer
Follow this Question
Related Questions
Enemy dies in editor mode but not in build 2 Answers
Health system in a text based game. 2 Answers
Why is this script not working? 2 Answers
how to put in a delay in void 1 Answer
SendMessage function not working 1 Answer