Search Unity

How to make player lose health when touch an enemy?

Discussion in 'Scripting' started by Tassilo593, Jul 11, 2020.

  1. Tassilo593

    Tassilo593

    Joined:
    May 6, 2020
    Posts:
    45
    hello i am making a 2d game and i made a health script for my game. i do not know how to code and i followed brackeyes tutorial. I just want replace the "if touch space then loose health" to something like "if touch collider2d with tag (tag) then loose health". Code for the health here :

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

    public class Health : MonoBehaviour
    {

    public Slider slider;

    public float currentHealth;

    void Start()
    {
    slider.value = currentHealth;
    slider.maxValue = currentHealth;
    }
    void Update()
    {
    if (Input.GetKey(KeyCode.Space))
    {
    TakeDamage(1);
    }
    }

    void TakeDamage(int damage)
    {
    slider.value -= damage;
    }
    }

    thank you and goodbye
     
  2. Tassilo593

    Tassilo593

    Joined:
    May 6, 2020
    Posts:
    45
    also how to like make an animation when the health goes to 0, and when the animation is finish : restart the game
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
  4. Tassilo593

    Tassilo593

    Joined:
    May 6, 2020
    Posts:
    45
    thanks!
     
  5. Tassilo593

    Tassilo593

    Joined:
    May 6, 2020
    Posts:
    45
    btw what is the pause key name on the xbox one controller?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Plug one in, iterate all the InputManager's keys, display them, and see which one moves.

    That's all I would do. If you read on the net that it is "XYZ" and you code up that and it doesn't work, you're going to go back to the above in any case. Just do it now, save a lot of time.
     
    Zer0Cool likes this.
  7. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    68
  8. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    Code (CSharp):
    1.    void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.         if (collision.gameObject.tag == "Enemy")
    4.         {
    5.             //collision.gameObject.SendMessage("ApplyDamage", 10);
    6.             TakeDamage(1);
    7.         }
    8.     }
    The other gameObject needs the tag "Enemy" to work.
    The part i comment out you can use if the enemy should take the damage and he has a ApplyDamage method.
     
  9. R3DB0ii

    R3DB0ii

    Joined:
    Mar 24, 2021
    Posts:
    1
    Hi thanks
    What parameter should I enter in "collision" when I call the function?
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    You do not call that function.

    The physics system inside of Unity will call that function for you.

    Unity will only call that when you meet ALL the conditions for it, as outlined in the documentation.

    When it calls, that collision will contain all the information you need about the collision that just happened.
     
  11. JustMeBrian

    JustMeBrian

    Joined:
    Jul 13, 2014
    Posts:
    16
    Maybe this could also help you.
     
  12. Tassilo593

    Tassilo593

    Joined:
    May 6, 2020
    Posts:
    45
    Thanks! I'll try A.S.A.P
     
  13. kbultaif

    kbultaif

    Joined:
    Nov 2, 2020
    Posts:
    1
    Hi, I am creating a top down game, i used the same code and is working perfectly fine, the problems come when, I hit an enemy, I take one damage, but then, if I stay steel, I will not suffer more damage, I was trying to give invulnerabilities frames or knockback, but couldnt find any code, can someone help please.
     
  14. aviralgoel

    aviralgoel

    Joined:
    Jul 12, 2017
    Posts:
    4
    You may be using OnCollisionEnter() or OnTriggerEnter().
    Use the function OnCollisionStay() or OnTriggerStay()