Lose Health when GameObject enters collider? (OnTriggerEnter)
So i have a script, and I have poked around for many hours but i cant find out how to make the health count decrease when a set gameObject enters the collider.
In my its a zombie and when the fps controller enters it will decrease in health.
The Is Trigger is checked.
anyone know how to do this? thanks and heres my script:
using UnityEngine;
using System.Collections;
public class HealthCount : MonoBehaviour {
public int health = 100;
void Update() {
Debug.Log (health);
if (health <= 0) {
Application.LoadLevel ("GameOver");
}
}
void OnTriggerEnter () {
health--;
}
}
This was one of my codes for the OnTriggerEnter $$anonymous$$ethod: void OnTriggerEnter () { if (gameObject.name == "FPSController") { health--; } }
Use this link to view the OnTriggerEnter $$anonymous$$ethod more clearly: http://pastebin.com/k054$$anonymous$$86i
void OnTriggerEnter(Collider other) { if (other.name == "FPSController") { health--; } }
@nitin22 Thanks it's working now, i'm not the best with c# yet I first started with unity nearly 3 years ago.
if you want to make it an answer then I will make it into a best answer.
Answer by Nitin22 · Jul 24, 2016 at 06:49 PM
void OnTriggerEnter(Collider other) { if (other.name == "FPSController") { health--; } }
You might want to edit your code. That looks like a mess.
Answer by $$anonymous$$ · Jul 24, 2016 at 07:03 PM
Try something like this, it will make your health decrease over time as long as you're inside the OnTriggerEnter.
using UnityEngine;
using System.Collections;
public class HealthCount : MonoBehaviour {
public int health = 100;
public float healthSpeedMultiplier = 0.25f;
void Update() {
Debug.Log (health);
if (health <= 0) {
Application.LoadLevel ("GameOver");
}
}
void OnTriggerEnter () {
health -= Time.deltaTime * healthSpeedMultiplier;
}
}
@$$anonymous$$ change the OnTriggerEnter to this:
Your answer
Follow this Question
Related Questions
OnTriggerEnter only works on one collision? 0 Answers
Who collided first 0 Answers
OnTrigger Problems Between Colliders Issue 1 Answer
Object refuses to collide 0 Answers