我在游戏中实施了各种各样的敌人但是现在,当我想为所有敌人添加预定义的动作时,我遇到了一个问题。
首先,管理敌人的代码运行良好。敌人只是等待,如果球员离他太近,他就会进攻。
[SerializeField]
private float rotationSpeed = 180;
[SerializeField]
private float movementSpeed = 1f;
[SerializeField]
private float meshRadius = 1f;
private IEnumerator turnTowardsPlayerCoroutine;
private IEnumerator moveTowardsPlayerCoroutine;
private bool isDead = false;
public float speed = 1;
private int maxLife = 3;
void OnTriggerEnter(Collider collider)
{
if (collider.gameObject.tag == "Player")
{
float playerDistance = Vector3.Distance(collider.transform.position, transform.position);
if (playerDistance >= 2f * meshRadius)
{
turnTowardsPlayerCoroutine = TurnTowardsPlayer(collider.transform);
moveTowardsPlayerCoroutine = MoveTowardsPlayer(collider.transform);
StartCoroutine(turnTowardsPlayerCoroutine);
StartCoroutine(moveTowardsPlayerCoroutine);
}
}
}
void OnTriggerExit(Collider collider)
{
if (collider.tag == "Player")
{
float playerDistance = Vector3.Distance(collider.transform.position, transform.position);
if (playerDistance >= 2f * meshRadius)
{
StopCoroutine(turnTowardsPlayerCoroutine);
StopCoroutine(moveTowardsPlayerCoroutine);
}
}
}
void OnDeath()
{
if (isDead)
{
return;
}
maxLife--;
if (maxLife <= 0)
{
isDead = true;
GameManager.Instance.NumKilledEnemies++;
StopCoroutine(turnTowardsPlayerCoroutine);
StopCoroutine(moveTowardsPlayerCoroutine);
Destroy(gameObject);
}
}
private IEnumerator TurnTowardsPlayer(Transform player)
{
while (true)
{
Quaternion targetRotation = Quaternion.LookRotation(player.position - transform.position, Vector3.up);
targetRotation.x = 0f;
targetRotation.z = 0f;
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
yield return 0;
}
}
private IEnumerator MoveTowardsPlayer(Transform player)
{
while (true)
{
Vector3 playerDirection = transform.position - player.position;
playerDirection.y = 0;
playerDirection = playerDirection.normalized;
Vector3 deltaMovement = playerDirection * movementSpeed * Time.deltaTime;
int layermask = LayerMask.GetMask("Environment");
Vector3 movingTowards = transform.position - playerDirection * meshRadius + (new Vector3(0f, 0.1f, 0f));
if (Physics.Raycast(movingTowards, Vector3.down, 0.25f, layermask))
{
transform.position -= deltaMovement;
}
yield return 0;
}
}
但在那之后,如果没有人接近他,我想添加一个预定义的敌人运动,所以我做了这样的事情:
private IEnumerator moveDefaultCoroutine;
void Start()
{
moveDefaultCoroutine = MoveToPosition(new Vector3(transform.position.x, transform.position.y, transform.position.z + 3), 5);
StartCoroutine(moveDefaultCoroutine);
}
private IEnumerator MoveToPosition(Vector3 newPosition, float time)
{
while (true)
{
float elapsedTime = 0;
Vector3 startingPos = transform.position;
while (elapsedTime < time)
{
transform.position = Vector3.Lerp(startingPos, newPosition, (elapsedTime / time));
elapsedTime += Time.deltaTime;
yield return null;
}
yield return new WaitForSeconds(2);
elapsedTime = 0;
while (elapsedTime < 0.8)
{
transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, new Vector3(0, 180, 0), (elapsedTime / 0.8f));
elapsedTime += Time.deltaTime;
yield return null;
}
elapsedTime = 0;
while (elapsedTime < time)
{
transform.position = Vector3.Lerp(newPosition, startingPos, (elapsedTime / time));
elapsedTime += Time.deltaTime;
yield return null;
}
yield return new WaitForSeconds(2);
elapsedTime = 0;
while (elapsedTime < 0.8)
{
transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, new Vector3(0, 0, 0), (elapsedTime / 0.8f));
elapsedTime += Time.deltaTime;
yield return null;
}
}
}
这就出现了问题,当我添加此代码时,敌人会随机消失......我做错了什么?
此处的完整代码:http://pastebin.com/tzGczv26
先谢谢!
PokeRwOw