Unity关于手游端摇杆移动、摇杆按钮冲突问题

Unity关于手游端摇杆移动、摇杆按钮冲突问题

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、摇杆移动问题

1.问题重现

①摇杆移动时,通过控制角色的刚体运动。
②当摇杆移动到游戏区域外松开时,后面对摇杆进行操作无响应。
③使用的是Drag事件来写的拖动逻辑。

2、解决

解决思路:摇杆控制角色移动时,不能通过控制角色的刚体移动,而要通过改变角色的位置来移动。

(有BUG)刚体移动代码如下(hor、ver为获取了摇杆移动的方向,判断设置了区间-1,1):

 	direction.x = hor;
	direction.y = ver * 0.7f;
	
	rig.velocity = direction * moveSpeed * 50 * Time.deltaTime;
        if (rig.velocity.x >= 0.05)
        {
   
            transform.rotation = new Quaternion(0, 0, 0, 0);
        }
        else if (rig.velocity.x <= -0.05)
        {
   
            transform.rotation = new Quaternion(0, 180, 0, 0);
        }

(可用)使用Translate的移动代码如下:

        direction.x = hor;
        direction.y = ver * 0.7f;
        player.transform.Translate(direction*moveSpeed*Time.deltaTime);

(可用)最后附上完整代码:

   private float moveSpeed;//移动速度
    private Vector2 direction;//移动方向
    private PlayerBehaviour behaviour;//角色行为
  //  public Transform Yingzi;//影子

    public Transform big; //摇杆背景
    public Transform small;//摇杆中心
    private float radius = 250;//摇杆半径

    private Vector2 moveCenter;//移动中心
    private Vector2 mouseToCenterVect;//鼠标移动到中心的向量
    private float mouseToCenterDistance;//鼠标到中心点的距离
    private float hor;//获取水平方向
    private float ver;//获取垂直方向
    public Transform player;//主角

// Start is called before the first frame update
    void Start()
    {
   
        behaviour = player.GetComponent<PlayerBehaviour>();
    }
    /// <summary>
    /// 开始拖动时
    /// </summary>
    public void 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值