- Home /
hi I know that this script is seen often but I have an another problem with a damage and health scripts. I think it's the GetComponent part, well anyway if you could help that would be great.
/PlayerHealth/
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLength;
void Start () {
healthBarLength = Screen.width / 2;
}
void Update () {
AddjustCurrentHealth(0);
}
void OnGUI(){
GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
}
public void AddjustCurrentHealth(int damage) {
curHealth -= damage;
if(curHealth < 0)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
if(curHealth == 0)
{
Destroy(gameObject);
}
healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
/bullet/
using UnityEngine; using System.Collections;
public class bullet : MonoBehaviour { public int Damage = 10; public float Speed = 1500f; public float lifetime = 5f;
void Start()
{
rigidbody.AddRelativeForce(new Vector3(0,0,Speed), ForceMode.Impulse);
Destroy(gameObject, lifetime);
}
void Update()
{
}
void OnTriggerEnter (Collider col)
{
print("y1");
if(col.tag == "Player")
{
col.gameObject.SendMessageUpwards("AddjustCurrentHealth", Damage, SendMessageOptions.DontRequireReceiver);
print("y2");
}
}
}
just their no script error but when the physical bullet collides nothing happens to the player's health, the player and the bullet are trigger.
Try not making the player a trigger. What type of collider are using on the player object?
it work when my gun is touching, but else nothing happens when they hit
Answer by bbbbb123 · May 02, 2013 at 01:59 AM
hi everyone I figured out the problem, I put my bullet speed to fast for the game engine to compute it. the script above works perfectly fine in C#. thank you to everyone that help and those who even attempts.
Answer by iwaldrop · Feb 08, 2013 at 05:27 AM
You are passing a negative value as damage and then subtracting it on the other side as well. Pass a positive damage value to your health script.
thx for that but it not even colliding. I will change it here too
How fast is your bullet moving? If it's anywhere close to the speed of a real bullet, you need to change it to a Raycast and calculate damage that way.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
bullet damage problem 1 Answer
Distribute terrain in zones 3 Answers
C# add helth over time 1 Answer
My Photon health script doesnt work 2 Answers