Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

player Health system [NEED HELP]

Discussion in 'Scripting' started by G4laxyExt, Apr 6, 2015.

  1. G4laxyExt

    G4laxyExt

    Joined:
    Apr 2, 2013
    Posts:
    15
    I'm really bad at scripting I started this week. I want to make a game a little bit like dayz (I know everybody is doing survival games). I want to make loose health to my character when he is getting hit by a game object called : "zombie" here is my script :

    #pragma strict

    var health : int = 100; //vie du du joueur
    var healthtext : GUIText; //text qui affiche le nombre de vie

    function Start () {
    health=100; //vie de départ du joueur
    }
    function OnCollisionEnter ( collision : Collision)
    {
    if(Collision.gameObject.name == "zombie"); //si je joueur rentre en colision avec un zombie
    {
    health -= 10; // le joueur pert 10 de vie
    print ("hit"); //écrit dans la commande Hit pour nous dire que le joueur est touché
    }
    }
    function Update ()
    {
    if(health < 1) //si la vie de notre personnage est plus bas que 1 (0)
    Application.LoadLevel(3); //loader le level de mort
    }
    and btw my notes in the scripte are in french
     
  2. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    Erm...it looks fine...what do you need help with?
     
  3. G4laxyExt

    G4laxyExt

    Joined:
    Apr 2, 2013
    Posts:
    15
    When I colide with the gameobject called : zombie, the print don't work and my character is not loosing health
     
  4. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    Well for starters, get rid of your Start function, thats not needed as your health and value has already been defined, so bin that just to be certain.

    The gameobject that has this script will need a collider set to 'On trigger'.

    That might do it, the script is certainly correct, i think its the colliders of the gameobject for etheir the player or the zombie. If the above solution dosnt work, try play with the colliders and have the zombie host the script...long shot, been awhile since i played with colliders

    Sorry dude
     
  5. G4laxyExt

    G4laxyExt

    Joined:
    Apr 2, 2013
    Posts:
    15
    Still not working... thanks for helping, do you think if I made a script on my player for his health and make a script for the zombie making me loosing health would work ?
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    I suspect your problem lies in the way you've set up your colliders, rather than with your script, though if you are using trigger colliders I think you need to use OnTriggerEnter instead of OnCollisionEnter.

    I believe there's an example of touch-damage in the Unity "nightmare" survival shooter example game; have you considered following that?
     
  7. G4laxyExt

    G4laxyExt

    Joined:
    Apr 2, 2013
    Posts:
    15
    I've been trying with trigger colider and normal colider but both are not working and I will try to take the unity survival shooter script
    thanks for helping
     
  8. MarkFowler

    MarkFowler

    Joined:
    Nov 29, 2012
    Posts:
    31
    I do the same with my game. Use a collider, make it a trigger and do as suggested above OnTriggerEnter. For that though, one of the objects needs a rigidbody attached to it (or both).
     
  9. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Remove the semi colon from the end of your if statement in the collision function
     
  10. MarkFowler

    MarkFowler

    Joined:
    Nov 29, 2012
    Posts:
    31
    Good eye, lordconstant.
     
  11. G4laxyExt

    G4laxyExt

    Joined:
    Apr 2, 2013
    Posts:
    15
    I reworte all my script and it's working now thanks for the helping