- Home /
Decreasing Player Health On Collision with Enemy
I'm wondering how I can get my script to work (In Java).
My HealthBar Script is as Follows (Using Unity 4.6 Canvas for the HealthBar):
var healthbar : GameObject;
var health : float;
var playerController : GameObject;
function Start () {
}
function Update () {
playerController = GameObject.FindGameObjectWithTag("Player");
healthbar.transform.localScale.x = health;
}
and my Zombie AI function to decrease the Player's Health is:
function OnCollisionEnter(collision: Collision) {
if (collision.gameObject.tag == "Player"){
healthbar.health -= 0.1;
}
}
How would I go about on the Zombie's collision with the Player decreasing the health for the Player?
First initialize health variable with some float value.
The value of the Float is the X Value, which currently is 0.7. The problem I'm facing is that when the Zombie Collides with my FPSController there is no health deduction.
first check whether ur if condition is working or not .. use debug.log to check..
Also, just a note:
if(collision.gameObject.tag == "TagName")
takes about twice as long to calculate as
if(collision.gameObject.CompareTag("TagName"))
does.
With a small and simple script, it won't matter much, but it's still generally handy to know.
Answer by aidengaming123 · Jun 12, 2015 at 06:33 AM
Is the OnCollisionEnter on the zombie? Because it needs to be in the same script as the player's in order to change the health variable. I recommend tagging your zombie as "Enemy", and putting this on your player (not tested)
var healthbar : GameObject;
var health : float = 10;
var playerController : GameObject;
function Start () {
}
function Update () {
playerController = GameObject.FindGameObjectWithTag("Player");
healthbar.transform.localScale.x = health;
}
function OnCollisionEnter(collision: Collision) {
if (collision.gameObject.tag == "Enemy"){
healthbar.health -= 0.1;
Debug.Log(health);
}
}
Answer by Hamza-Abdullah · Jun 12, 2015 at 06:33 AM
Add a capsule collider to the Zombie and set it to "Is trigger" And instead of using:
function OnCollisionEnter(collision: Collision) {
if (collision.gameObject.tag == "Player"){
healthbar.health -= 0.1;
}
}
use:
function OnTriggerEnter(collision: Collider) {
if (collision.gameObject.tag == "Player"){
healthbar.health -= 0.1f;
}
}
Now should be good to go!
I've made suggested changes and am getting error: Assets/zombieAI2.js(10,20): BCE0020: An instance of type 'healthbar' is required to access non static member 'health'.
$$anonymous$$y Scripts Currently are as Follows: Healthbar.js #pragma strict var healthbar : GameObject; var health : float = 0.7; var playerController : GameObject;
function Start () {
}
function Update () { playerController = GameObject.FindGameObjectWithTag("Player");
}
ZombieAI2.js (Just Collision Function) function OnTriggerEnter(collision: Collider) {
if (collision.gameObject.CompareTag("Player")){
healthbar.health -= 0.1f;
}
}
Anyone Have any Clues?
Your answer
Follow this Question
Related Questions
Lose health on collision 1 Answer
Fist Punch Collision 0 Answers
Enemy detection in the light(Javascript) 1 Answer
Have script detect which collider 2 Answers