嘿,我想让我的球员变得无敌,当他受到伤害并失去生命的时候。我不能让我的玩家变得无敌,而且如果他撞上了几个伤害建筑,他会受到比预期更多的伤害,我希望他失去20%的生命值,然后在2-3秒内保持无敌。
这是附在我的伤害结构上的
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoseHealth : MonoBehaviour {
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
HealthBarScript.health -= 20f;
}
}
}这是我在画布上的FullHearts,像个孩子一样依附在我“空虚的心”上。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class HealthBarScript : MonoBehaviour {
Image FullHearts;
float maxHealth = 100f;
public static float health;
// Use this for initialization
void Start () {
FullHearts = GetComponent<Image>();
health = maxHealth;
}
// Update is called once per frame
void Update () {
FullHearts.fillAmount = health / maxHealth;
if (health == 0)
{
Application.LoadLevel(Application.loadedLevel);
}
}
}我在我的playerscript上有这个,它不起作用,所有其他的脚本都在起作用,但只需要它只有一颗心就会失去,无论玩家是否击中了多个结构,然后是不可抗拒的,有时间恢复而不是击中结构:
if (!invincible)
{
if (col.gameObject.tag == "enemy")
{
// HealthBarScript.health -= 20f;
// health -= 20; // subtract 1 form your total health
invincible = true; // makes this whole function unusable since invincible is no longer false
new WaitForSeconds(3);
invincible = false; // makes this whole function reusable since invincible is false again
}
}发布于 2018-11-15 21:47:33
所以我用你提供的方法做了一个简单的碰撞‘游戏’,一切都很正常。但是现在,当我看得更远的时候,我发现你提供的东西中有一些冗余,你的“答案”有点令人困惑。
但是你的第一个脚本显示,每次伤害交易者与“玩家”碰撞时,它都会造成伤害。
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
HealthBarScript.health -= 20f;
}
}每当“玩家”与“敌人”相撞时,它会做一个无敌的检查,这应该是有效的。(假设注释已删除)。但是,正如评论所说,“玩家”击中伤害结构不会造成伤害,但总是(不管是不是无敌的)伤害结构对玩家造成伤害。
if (!invincible)
{
if (col.gameObject.tag == "enemy")
{
// HealthBarScript.health -= 20f;
// health -= 20; // subtract 1 form your total health
invincible = true; // makes this whole function unusable since invincible is no longer false
StartCoroutine(Invincible()); // makes this whole function reusable since invincible is false again
}
}
}
IEnumerator Invincible()
{
if (invincible == true)
{
yield return new WaitForSeconds(2);
{
invincible = false;
}
}但是,当你在无敌检定到期前再次命中时,敌方的脚本仍在造成伤害。
你有两个脚本在造成破坏,而只有一个脚本做了无敌检查。
去掉结构上的破坏性脚本,正确地标记所有的结构,在播放器上注释脚本,你就可以开始工作了。或者你可以把这个脚本保存在伤害结构上,然后让每一个都通过使布尔值静止来检查玩家是否是无敌的。
你也可以用Debug.Log的标准用法很容易地回答你自己的问题(“到底发生了什么”)。Debug.Log应该是你最好的朋友。
https://stackoverflow.com/questions/53318861
复制相似问题