【发布时间】:2020-11-16 17:44:09
【问题描述】:
我正在制作一款塔防游戏(根据 Brackeys 教程),我想制作它,以便当敌人遇到障碍物或前面的敌人时,他们会停下来。当前面的障碍物或敌人被摧毁时,我希望他们再次前进。
我正在为这些敌人使用基于节点的传输,来自 Brackeys 教程。到目前为止,它非常有帮助,我正处于想要在游戏中添加额外内容的时刻。
澄清一下,敌人遇到障碍物或其他敌人时确实会停下来,之后他们就不会再移动了。敌人也有一个刚体,所以他们可以检测到触发器,并且障碍物和敌人都有一个触发器碰撞器。
这是所有的敌人移动代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Enemy))]
public class EnemyMovement : MonoBehaviour
{
private Transform target;
private int wavepointIndex = 0;
EnemyTurret eTurret;
private Enemy enemy;
public bool canMove = true;
//private CapsuleCollider _cc;
private void Start()
{
enemy = GetComponent<Enemy>();
target = WaypointScript.points[0];
}
private void OnTriggerEnter(Collider col)
{
if (col.CompareTag("Obstacle") || col.CompareTag("Enemy"))
{
canMove = false;
Debug.Log("Stop");
}
}
private void OnTriggerStay(Collider col)
{
if (col.CompareTag("Obstacle") || col.CompareTag("Enemy"))
{
if (col.GetComponent<Enemy>().health <= 0)
{
Movement();
Debug.Log("Start Moving!");
}
}
}
void Movement()
{
canMove = true;
}
private void Update()
{
if (canMove == true)
{
Vector3 dir = target.position - transform.position;
transform.Translate(dir.normalized * enemy.speed * Time.deltaTime, Space.World);
if (Vector3.Distance(transform.position, target.position) <= 0.4f)
{
GetNextWayPoint();
}
enemy.speed = enemy.startSpeed;
}
}
void GetNextWayPoint() //Sets the new waypoint to the enemy, however when it finishes it's cycle of waypoints, the enemy is destroyed.
{
if (wavepointIndex >= WaypointScript.points.Length - 1)
{
EndPath();
return;
}
wavepointIndex++;
target = WaypointScript.points[wavepointIndex];
}
void EndPath()
{
canMove = false;
//PlayerStats.Health--;
//Destroy(gameObject);
} ```
【问题讨论】:
-
会不会是你在敌人生命值降到0时摧毁了对撞机,所以OnTriggerStay中第二个条件语句的代码没有被执行?
-
@Sam 禁用它?这样它就可以再次停下来,也许会有一点延迟,以便敌人之间再次出现间隙?
-
你很可能在当前敌人有时间再次开始移动之前摧毁了生命值为 0 的敌人。虽然代码不完整,但仍然缺少一些敌人和障碍物代码。当敌人的生命值为 0 时会发生什么?
-
但是,你可以试试。对不起,我以为你不明白我的问题
-
一个快速的解决方法是在敌人被摧毁之前添加一个短暂的延迟。 docs.unity3d.com/ScriptReference/Object.Destroy.html这里可以看到可以在destroy方法本身添加延迟。
标签: c# unity3d triggers tags boolean