Need a player health and damage script please!!!

Ok so i’ve been trying to create player heath script but it doesn’t work all.So if someone could make a player health script that can be damage and when the health goes to the bottom the player will be replaced by a different game object please it would really help.:frowning:

You should never ask for scripts.

Post what you got so far, and then people can help you out and give you hints!

1 Like

@BFGames , giving scripts isn’t ideal , but I know that people learn from looking at other scripts and trying to work out what they do and how they are coded.

Here’s a simple script of what you are asking , it’s nothing too fancy.

#pragma strict
var Health : int = 10;
var Character : GameObject;
var BulletTag : String;

function Start () {

}

function OnCollisionEnter ( collision : Collision ){

if(collision.gameObject.tag == BulletTag) //Write in the string what the tag of your bullet is.
{
Health--; //Subtracts health from the character.
Destroy(collision.gameObject); //This destroys the colliding object.

}

if(Health < 1){  // if the health of the character is below 1 then destroy the character.
Destroy(Character);
}
}
4 Likes

What for? Quickest way to get the answer (and one that will help you learn more by doing) is:
https://www.google.com.au/search?q=unity+player+health+and+damage+script

=

http://www.superspacetrooper.com/2012/05/free-unity3d-script-to-handle-weapon-damage-to-enemy-with-multiple-child-components/
Tutorials:

As a start for player script hints…
It helps if you write down on paper what you want before trying to code it… (eg Psuedocode)

What variables do you need:
Max health and Current health at a minimum in this case?

What else do you need? You need to get hurt and to die…
An example method to get hurt…
eg ApplyDamage(int damageToApply)
take damageToApply off current health
(Can be used to heal too… just put in a negative number)
If your current health is 0 or lower then you are dead (do you want current health to be able to go higher than max health? if not, then if current health is higher than max health (eg. healed for more than damaged) then reset current health to max health

Another method to die…
eg. KillPlayer
You will need to store the new (dead) model as a variable – As you have identified you need a new variable, you can add it to your list up the top now!
Instantiate the new model (separate game object will keep it there if you delete ‘this’ game object) Instantiating a prefab will also mean you can have scripts attached to it from the start to give it the behaviour you want (eg. break into parts, play a die animation then fade into the ground etc)
Delete old model/game object

So… as a (VERY) basic framework to go from (C#)

int maxHP;
int currentHP;
GameObject deathModel;

void Start()
{
 currentHP=maxHP;
}

void Update()
{

}

void ApplyDamage(int damageToApply)
{
 //You can work this out? Remember to have a check for HP below zero or above maxHP somewhere (doesnt have to be here)
}

void KillPlayer()
{
 //And should be able to work this out... Look through the above stuff
}

Finally you use the ApplyDamage part of the script by using SendMessage when your enemy damage causing element (bullet, sword, explosion, fire etc) hits the player

Set The Player collider to trigger.
Add box collider to enemy
public float playerhp = 20f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == “Enemy”)
{
playerhp -= 10;
}
if (playerhp <= 0f)
{
Die();
}
void Die ()
{
Destroy(gameObject);
}
}
}

I have problem with EnemyDamage and Player Health and idk why because I dont have any errors

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyDamage : MonoBehaviour
{
    public PlayerHealth playerHealth;
    public int damage = 20;


    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag == "Player")
        {
            playerHealth.TakeDamage(damage);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour
{
    public float health;
    public float maxHealth;
    public Image healthBar;
  

   // private bool isDead;
   // public GameManagerScript gameManager;


    void Start()
    {
        health = maxHealth;
    }
    public void TakeDamage(int amount)
    {

        health -= amount;


        healthBar.fillAmount = Mathf.Clamp(health / maxHealth, 0, 100);
        if(health <= 0)
        {
            Destroy(gameObject);
            //gameManager.gameOver();
           // gameObject.SetActive(false);
        }
    }
}

Typically you should make your own thread instead of piggybacking on an existing thread, especially an old thread. Secondly, you’re not really giving us much to go on.

What are you seeing that tells you you have a problem? Is damage not happening at all? It could be the wrong physics layers.

At a glance, one problem is that image.fillAmount takes a 0 - 1 range, but you’re clamping 0, 100.

Please don’t hijack and necropost. Please create your own threads for problems you may have.

Thanks.