unity3D,敌人跟随问题

时间:2016-04-29 22:02:30

标签: c# unity3d unityscript unity5

当玩家进入敌人的半径范围时,我试图让敌人跟随我的玩家,但当我的子弹击中object或进入radiusArea时,让敌人停止跟踪。

有关详细信息,请参阅我的gif:

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);
        }
    }
}

我希望有人可以帮助我:(

1 个答案:

答案 0 :(得分:1)

Physics2D.OverlapCircle仅检测具有最低z值的对撞机(如果多个在范围内)。因此,您需要更改z值,以便播放器具有最低值,或者您需要使用Physics2D.OverlapCircleAll并检查列表以查找播放器。或者您可以更改图层,这样只有播放器本身位于您进入重叠测试的特定图层上。