- Home /
Why do the player's health points go down so fast?
In my 2D Platformer, I added health points. When I touch the enemy, my health points go down extremely rapidly. Here is my Player Controller script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Sprites;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
public float speed;
public float jumpSpeed;
public Rigidbody2D rb;
public Text coinText;
public Text healthText;
public int maxHealth;
private Vector3 movement;
private bool canJump=false;
private int coinAmount=0;
private int healthPoints;
void Start ()
{
healthPoints = maxHealth;
rb = GetComponent<Rigidbody2D>();
bool canJump = true;
coinText.text = "Coins: " + coinAmount;
healthText.text = "Health: " + healthPoints + "/" + maxHealth;
}
void Update ()
{
transform.rotation = new Quaternion (0.0f, 0.0f, 0.0f, 0.0f);
if (Input.GetKey (KeyCode.A))
{
transform.Translate (-Vector3.right * Time.deltaTime * speed);
}
if (Input.GetKey (KeyCode.D))
{
transform.Translate (Vector3.right * Time.deltaTime * speed);
}
if (Input.GetKeyDown (KeyCode.Space)&&canJump)
{
rb.AddForce (transform.up * jumpSpeed);
canJump=false;
}
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Ground")
{
canJump=true;
}
if (col.gameObject.tag == "Enemy")
{
healthPoints = healthPoints - 1;
healthText.text = "Health: " + healthPoints + "/" + maxHealth;
if (healthPoints <= 0)
{
SceneManager.LoadScene (sceneName: "Game Over");
}
}
}
void OnCollisionStay2D (Collision2D col)
{
if (col.gameObject.tag == "Enemy")
{
healthPoints = healthPoints - 1;
healthText.text = "Health: " + healthPoints + "/" + maxHealth;
if (healthPoints <= 0)
{
SceneManager.LoadScene (sceneName: "Game Over");
}
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Coin")
{
other.gameObject.SetActive (false);
coinAmount = coinAmount + 1;
coinText.text = "Coins: " + coinAmount;
}
if (other.tag == "Void")
{
healthPoints = healthPoints - healthPoints;
healthText.text = "Health: " + healthPoints + "/" + maxHealth;
if (healthPoints <= 0)
{
SceneManager.LoadScene(sceneName:"Game Over");
}
}
}
}
Can anyone help me figure out why the player's health points go down so rapidly when they touch the enemy?
Answer by Nischo · Jun 28, 2017 at 05:14 PM
Check the documentation for OnCollisionStay2D. You see that its called every frame, if you are inside the collision shape of an enemy, this will be called until you leave it. But its called every frame, depending on your machine, this might be 30 times, 60 times or more per second.
@Nischo Now I understand the problem, but do you have an idea for a solution? I really need to know how to slow it down.
$$anonymous$$ultiple ways depends on what you want to achieve. First of all i would "repel" the user from the enemy so he can't actually enter "him" AddForce or something should do the trick. Usually you want to add something like a damage cooldown. In most 2D jump'n run games its when the player blinks after getting hit to show that he can't get hit again. Setting a boolean to true once hit, and resetting it back to false after a certain period of time is the most straightforward way i suppose .
Look up examples of how to enforce a maximum fireRate for shooting games. There's tons of them. You just need to familiarize yourself with the Time.time and Time.deltaTime to do a cooldown.
Re: your latest question... simply move this to your OnCollisionEnter2D, unless when the player is in this area health needs to constantly go down, in which case, you could actually multiply the health loss by Time.DeltaTime and that will slow the rate of health loss. You could also add a delay between health losses, or use a coroutine.
BTW, speaking of which, you have some weird code in there..
You know this: healthPoints = healthPoints - healthPoints;
Is basically this, right? healthPoints = 0;
Very trivial math here... any number $$anonymous$$us itself is 0. That also makes this part unnecessary:
if (healthPoints <= 0)
Don't overcomplicate things.