我的问题是我正在制作一个团结一致的游戏,而我想做的是当游戏中的敌人碰到障碍物时,它每秒造成x点伤害。
“桌子”障碍物上有一个对撞机和健康脚本,以及对敌人进行移动和攻击的对撞机和脚本。
敌人与桌面物体碰撞时会停止并造成伤害!但是,伤害是连续的……我尝试了协程和Invoke,但是所有结果都是相同的。每次触发检测到10点伤害,而不是每秒。
以下是脚本:
敌人的移动和攻击:
private Animator anim;
public GameObject Desk;
private DeskHealth deskHealth;
//private bool inQueue = false;
private bool collidingWithDesk;
public float timeAfterStart;
[Range(0,10)]
public float attackSpeedEnemy = 1f;
[Range(10,100)]
public float walkSpeed;
[SerializeField] float damageDealtToDesk = 10f;
void Start () {
anim = GetComponent<Animator>();
deskHealth = GetComponent<DeskHealth>();
collidingWithDesk = false;
}
void Update()
{
if (collidingWithDesk == false)
{
enemyIsWalking();
}
}
void enemyIsWalking()
{
transform.Translate(0, 0, (-walkSpeed * Time.deltaTime));
}
IEnumerator enemyIsAttacking()
{
var deskHealth = Desk.GetComponent<DeskHealth>();
deskHealth.DealDamageToDesk(damageDealtToDesk);
yield return new WaitForSeconds(5f);
}
void OnTriggerStay(Collider otherCollider)
{
if (otherCollider.tag == "Desk")
{
collidingWithDesk = true;
transform.Translate(0, 0, 0);
anim.Play("enemyHit");
StartCoroutine(enemyIsAttacking());
}
}
桌面健康:
[SerializeField] float deskHealth;
Animator anim;
public void DealDamageToDesk(float deskDamage)
{
deskHealth -= deskDamage;
if (deskHealth <= 0)
{
print("am ded");
//anim.Play("deskDestroyed");
}
}
答案 0 :(得分:1)
医生说的是:“对于每次触碰触发器的对撞机,每次物理更新都会调用一次OnTriggerStay。”这意味着您的代码:
ThisWorkbook.Worksheets("Data").Range("A1:A4000").Select
For Each cell In Selection
position = InStr(cell.Value, "(") - 1
cell.Value = Left(cell.Value, position)
Next cell
将在每次更新时被调用,然后由于if (otherCollider.tag == "Desk")
{
collidingWithDesk = true;
transform.Translate(0, 0, 0);
anim.Play("enemyHit");
StartCoroutine(enemyIsAttacking());
}
而暂停5秒钟。您应该使用OnColliderEnter,否则最好使用OnTriggerEnter,因为此一次将被调用一次。然后,如果您每秒需要一次,将WaitForSeconds调用中的值更改为1。
此外,正如Draco18s所规定的那样,您的产量无所作为。 您应该这样做:
yield return new WaitForSeconds(5f);