[英]How can I stop the enemy from instantly killing the player?
I am trying to get the player health script to decrease health value once the enemy shoots the player game object with the enemy projectile script How can I get this health script to work? 一旦敌人用敌方投射脚本射击了玩家游戏对象,我就试图让玩家健康脚本降低健康值,我如何才能使该健康脚本起作用? I attached the script to the player but the player game object dies instantly when shot.
我将脚本附加到了播放器上,但是播放器中的播放器游戏对象立即死亡。 I added a collision method to get component projectiles am I on the right path?
我添加了一种碰撞方法来获得部件弹丸,我是否走在正确的道路上? any feedback is welcomed.
欢迎任何反馈。
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerHealth : MonoBehaviour
{
[SerializeField] GameObject deathFX;
[SerializeField] Transform parent;
public Image Bar;
public Text Text;
public float max_health = 100f;
public float cur_health = 0f;
//Use this for initialization
void Start()
{
// Initialize the health that is given
cur_health = max_health;
InvokeRepeating("decreaseHealth", 0f, 2f);
}
void Update()
{
if (cur_health <= 0)
{
Destroy(gameObject); //if the player has no health point left, destroy the player.
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.GetComponent<Projectiles>())
{
max_health -= cur_health;
//if the collision object has a homing script, minus player health by damageToPlayer
}
}
void decreaseHealth()
{
//Subtract the health at the following rate
//Check if the health is 0 before we do any damage
if (cur_health < 0)
{
cur_health = 0;
}
//make a new variable and divide the current health my the maximum health
//this is because the fill value goes from 0 to 1
float calc_health = cur_health / max_health; // 70 / 100 = 0.7
SetHealth(calc_health);
//Change the color of the health bar
if (cur_health != 0 && cur_health <= max_health / 1.6 && cur_health > max_health / 2.9) // on the scale of 1000, if health <= 625 and is greater than 345, do the following
{
Bar.color = new Color32(171, 162, 53, 255);
}
else if (cur_health != 0 && cur_health <= max_health / 2.9) // on the scale of 1000, if health <= 625, do the following
{
Bar.color = new Color32(158, 25, 25, 255);
}
}
void SetHealth(float myHealth)
{
//defill the bar based on the current health
Bar.fillAmount = myHealth;
//change the text to display the amount of health
Text.text = cur_health.ToString("f0") + "/100";
}
}
Maybe I am completely misunderstanding the question, but I think all you need is an if statement. 也许我完全误解了这个问题,但是我认为您所需要的只是一个if语句。
public void ApplyDamage(int damageToTake)
{
Debug.Log(Time.frameCount + ": ApplyDamage was called.");
currentHealthPlayer -= damageToTake;
if (currentHealthPlayer <= 0) //Only apply death effect if health goes to zero
{
Debug.Log("Player met an unfortunate end. Time of death: " + Time.frameCount);
GameObject fx = Instantiate(deathFX, transform.position, Quaternion.identity);
fx.transform.parent = parent;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.