当玩家进入敌人的半径范围时,我试图让敌人跟随我的玩家,但当我的子弹击中object
或进入radiusArea
时,让敌人停止跟踪。
有关详细信息,请参阅我的gif:
脚本:
using UnityEngine;
using System.Collections;
public class FlyEnemyMove : MonoBehaviour
{
public float moveSpeed;
public float playerRange;
public LayerMask playerLayer;
public bool playerInRange;
PlayerController thePlayer;
// Use this for initialization
void Start()
{
thePlayer = FindObjectOfType<PlayerController>();
}
// Update is called once per frame
void Update()
{
flip();
playerInRange = Physics2D.OverlapCircle(transform.position, playerRange, playerLayer);
if (playerInRange)
{
transform.position = Vector3.MoveTowards(transform.position, thePlayer.transform.position, moveSpeed * Time.deltaTime);
//Debug.Log(transform.position.y);
}
//Debug.Log(playerInRange);
}
void OnDrawGizmosSelected()
{
Gizmos.DrawWireSphere(transform.position, playerRange);
}
void flip()
{
if (thePlayer.transform.position.x < transform.position.x)
{
transform.localScale = new Vector3(0.2377247f, 0.2377247f, 0.2377247f);
}
else
{
transform.localScale = new Vector3(-0.2377247f, 0.2377247f, 0.2377247f);
}
}
}
我希望有人可以帮助我:(
答案 0 :(得分:1)
Physics2D.OverlapCircle
仅检测具有最低z值的对撞机(如果多个在范围内)。因此,您需要更改z值,以便播放器具有最低值,或者您需要使用Physics2D.OverlapCircleAll
并检查列表以查找播放器。或者您可以更改图层,这样只有播放器本身位于您进入重叠测试的特定图层上。