【问题标题】:unity3D, enemy following issueunity3D,敌人跟随问题
【发布时间】:2016-04-30 22:06:15
【问题描述】:

我试图在玩家进入敌人的半径区域时让敌人跟随我的玩家,但是当我的子弹击中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);
        }
    }
}

希望有人能帮帮我:(

【问题讨论】:

  • 你的 gif 不工作,伤心!
  • 就像乔说的那样,您的图片链接不起作用。我修好了它。请务必测试您的图片链接时间。

标签: c# unity3d unityscript unity5


【解决方案1】:

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

【讨论】:

  • 您的 gif 实际上看起来敌人由于其他原因停止跟随玩家。在它停止的那一刻,除了玩家移动之外什么都没有发生。敌人是不是被什么东西卡住了?你检查playerInRange 是否仍然是真的? (例如,放置一个 Debug.Log。)
猜你喜欢
  • 1970-01-01
  • 2015-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多