2D游戏中怎样移动 敌人移动到玩家面前

2D游戏中 敌人移动到玩家面前

//放到循环里
  public  override  void  Move( Transform player, Transform npc)
        {
            //旋转方向
            LooakAtPlayer(npc,player);
            //移动
               
            float dist = GetDis(player,npc);
            float y = 0;
            float x = 0;
            if ( !isArriveDisIn(player,npc))
            {
                //先x轴移动
                float xValue = Mathf .Abs((npc.transform.localPosition.x-player.transform.localPosition.x));
                if (xValue <= date.arriveDistanceX) //是否接近
                {
                    x = 0;
                }
                else //距离玩家远
                {
                    x = 1;
                }
                //在Y轴移动
                float yValue = Mathf .Abs((npc.transform.localPosition.y - player.transform.localPosition.y));
                if (yValue < date.arriveDistanceY) //在一行
                    {
                        y = 0f;
                    }
                    else //不和玩家在一行
                    {
                        //玩家在我的上还是下呢
                        if (npc.transform.localPosition.y > player.transform.localPosition.y) //我在玩家上面
                        {
                            y = -1f;
                        }
                        else //我在玩家下面
                        {
                             y = 1f;
                        }
            
                    }
              // Debug.LogError(xValue+ "  =xValue,yValue= " + yValue);
                Vector3 dir = new Vector3 (x, y, 0) * Time .deltaTime * curSpeed;
                npc.transform.Translate(dir);
            }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值