Open jordode opened 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
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();
}
}
That is the health display, working system coming soon. (We just need the collision to subtract 1 from countText.text)
//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();
}
}
//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);
}
}
}
good job
Health changes based on collision