jordode / Chibi-Knight-2-The-Better-Version

3 stars 1 forks source link

Simple Health System #1

Open jordode opened 6 years ago

jordode commented 6 years ago

Health changes based on collision

Interwebsurfer commented 6 years ago

We're using a simple count system like you would if you were picking up coins, but instead of adding points we're subtracting health, also the character starts out with an amount of health instead of 0

Interwebsurfer commented 6 years ago

using UnityEngine; using System.Collections; using JetBrains.Annotations; using UnityEngine.UI;

public class YesDaddyPlz : MonoBehaviour { private Rigidbody2D rb2d; private int count; public Text countText;

void Start()
{
    rb2d = GetComponent<Rigidbody2D>();
    count = 2;
    SetCountText();
}

void FixedUpdate()
{
    float moveHorizontal = Input.GetAxis("Horizontal");

    Vector2 movement = new Vector2(moveHorizontal, 0.0f);
    rb2d.AddForce(movement);
}

private void OnTriggerEnter2D(Collider2D other)
{
    if (other.gameObject.CompareTag("Hurt"))
    {
        other.gameObject.SetActive(false);
        count = count - 1;
        SetCountText();
    }
}

void SetCountText()
{
    countText.text = "Health: " + count.ToString();
}

}

Interwebsurfer commented 6 years ago

That is the health display, working system coming soon. (We just need the collision to subtract 1 from countText.text)

Interwebsurfer commented 6 years ago

//This is the player code, with health using UnityEngine; using System.Collections; using JetBrains.Annotations; using UnityEngine.UI;

public class YesDaddyPlz : MonoBehaviour { private Rigidbody2D rb2d; private int count; public Text countText;

void Start()
{
    rb2d = GetComponent<Rigidbody2D>();
    count = 2;
    SetCountText();
}

void FixedUpdate()
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    Vector2 movement = new Vector2(moveHorizontal, 0.0f);
    rb2d.AddForce(movement);
}

private void OnTriggerEnter2D(Collider2D other)
{
    if (other.gameObject.CompareTag("Triangle"))
   //Triangle is the name of the tag, our enemy was a triangle so we named it triangle

{ count = count - 1; SetCountText(); } }

void SetCountText()
{
    countText.text = "Health: " + count.ToString();
}

}

Interwebsurfer commented 6 years ago

//Updated healthsystem using UnityEngine; using System.Collections; using JetBrains.Annotations; using UnityEngine.SceneManagement; using UnityEngine.UI;

public class YesDaddyPlz : MonoBehaviour { private Rigidbody2D rb2d; private int count; public Text countText; public Text loseText;

void Start()
{
    rb2d = GetComponent<Rigidbody2D>();
    count = 3;
    loseText.text = "";
    SetCountText();
}

void FixedUpdate()
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    Vector2 movement = new Vector2(moveHorizontal, 0.0f);
    rb2d.AddForce(movement);
}

private void OnTriggerEnter2D(Collider2D other)
{
    if (other.gameObject.CompareTag("Triangle"))
    {
        count = count - 1;
        SetCountText();

    }
}

void SetCountText()
{
    countText.text = "Health: " + count.ToString();
    if (count <= 0)
    {
        loseText.text = "You lose :(";
        SceneManager.LoadScene(1);
    }
}

}

Nyancatisfat commented 6 years ago

good job