Unity - A计划(一年有效期) 扫二维码继续学习 二维码时效为半小时

(61评价)
价格: 2208.00元
《拾荒者》案例Enemy的移动问题
塔里木河丶发起了问答2018-05-22
1
回复
689
浏览

老师,我在做拾荒者的控制敌人移动课程时,出现了敌人全部移动到左下角的问题,下面是问题截图,然后下面是Enemy的代码

  private Vector2 targetPosition;
    private Transform player;
    private Rigidbody2D rigidbody;

    public float smoothing = 3;

    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
        rigidbody = GetComponent<Rigidbody2D>();
        targetPosition = transform.position;
        GameM.Instance.enemylist.Add(this);
    }

    private void Update()
    {
        rigidbody.MovePosition(Vector2.Lerp(transform.position, targetPosition, smoothing * Time.deltaTime));

    }


    public void Move()
    {
        Vector2 offset = player.position - transform.position;
        if (offset.magnitude < 1.1f)
        {
            //攻击
        }
        else
        {
            float x = 0;
            float y = 0;
            
            if (Mathf.Abs(offset.y) > Mathf.Abs(offset.x))
            {
                //向Y轴移动
                if (offset.y < 0)
                {
                    y = -1;
                }
                else {
                    y = 1;
                }

            }
            else
            {
                if (offset.x > 0)
                {
                    x = 1;
                }
                else {
                    x = -1;
                }
            }
            targetPosition += new Vector2(x,y);
        }
所有回复
发表回复
你还没有登录,请先 登录或 注册!