【发布时间】:2019-09-13 20:41:47
【问题描述】:
你好,我试图用if(Vector2.Distance(transform.position, player.position) > minDistance)让我的敌人停在玩家面前
但这并没有按我的预期工作。敌人应该开始在一定范围内跟随玩家,而不是停在他面前。这与if(Vector2.Distance(transform.position, player.position) <= range) 工作得很好
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public int Health;
public Transform player;
public float range;
public float speed;
public Animator anim;
public Transform goblinRange;
public float attackRangeGoblin;
public LayerMask whatIsPlayer;
public int damage;
private float timeBtwAttack;
public float startTimeBtwAttack;
// Update is called once per frame
void Update()
{
if (Vector2.Distance(transform.position, player.position) <= range)
{
anim.SetBool("GWalking", true);
transform.position = Vector2.MoveTowards(transform.position, player.position, speed *
Time.deltaTime);
Collider2D[] playerToDamage = Physics2D.OverlapCircleAll(goblinRange.position,
attackRangeGoblin, whatIsPlayer);
for (int i = 0; i < playerToDamage.Length; i++)
{
playerToDamage[i].GetComponent<PlayerMovement>().TakeDamage(damage);
}
}
else
{
Idle();
}
}
private void FixedUpdate()
{
if (Health <= 0)
{
Destroy(gameObject);
}
}
private void Idle()
{
anim.SetBool("GWalking", false);
}
public void TakeDamage(int Damage)
{
Health -= Damage;
Debug.Log("Damage Taken");
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(goblinRange.position, attackRangeGoblin);
}
private void Attack()
{
anim.SetTrigger("goblinAttack");
}
}
所以我需要敌人停在我的玩家面前并开始攻击动画,但我不知道如何在不删除“if(Vector2.Distance(transform.position, player.position) > minDistance)。任何帮助将不胜感激。
【问题讨论】:
-
我不确定我是否理解您的问题。
if(Vector2.Distance(transform.position, player.position) > minDistance)的意思是“如果对手距离玩家的距离超过 minDistance ,你确定这就是你想要的吗?你是说只有在这种情况下才跑?
标签: c# unity3d artificial-intelligence