unity虚拟摇杆控制的实现

一、前言

手机游戏,经常会用到摇杆来控制角色,本文记录了如何在unity中用比较简单的方法制作摇杆,并且实现摇杆控制角色左右移动。

二、需要继承的类

  • ScrollRect(滚动类,一般用于实现UI的拖拽滚动,包含在UnityEngine.UI中)
  • IPointerDownHandler(指针按下类,实现接口检测指针是否按下,包含在UnityEngine.EventSystems中)
  • IPointerUpHandler(指针抬起类,实现接口检测指针是否抬起,需要和IPointerDownHandler的接口一起实现才会发挥作用,包含在UnityEngine.EventSystems中)

三、虚拟摇杆的制作

需要图片:

  • 虚拟摇杆的背景图
  • 摇杆图

在canvas中新建一个image对象,把虚拟摇杆的背景图放上去,在该对象中新建一个子image作为摇杆,把摇杆图放上去,调整好大小和位置即可。

 四、摇杆控制角色移动

新建一个脚本Stick,输入以下代码,挂载到摇杆背景上,并把摇杆对象拖到脚本的content处。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;


public class Stick : ScrollRect,IPointerUpHandler,IPointerDownHandler
{
    protected float mRadius = 0f;//摇杆范围半径

    protected override void Start()
    {
        base.Start();//原start方法

        //计算摇杆范围半径
        mRadius = (transform as RectTransform).sizeDelta.x * 0.45f;
    }

    public override void OnDrag(PointerEventData eventData)
    {
        base.OnDrag(eventData);//原onDrag方法

        //摇杆位置
        var contentPosition = content.anchoredPosition;
        
        if (contentPosition.x > 0)
        {
            //在此写控制角色右移逻辑
        }
        else if(contentPosition.x < 0)
        {
            //在此写控制角色左移逻辑
        }
        else
        {
            //在此写控制角色停止移动逻辑
        }

        //如果摇杆位置超出范围,则限制在范围内
        if(contentPosition.magnitude > mRadius)
        {
            contentPosition = contentPosition.normalized * mRadius;
            SetContentAnchoredPosition(contentPosition);
        }
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        //这个接口需要实现用来检测指针是否点击,什么都不写也行,有就ok
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        //在此写控制角色停止移动逻辑
    }
}

 

  • 9
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,针对您的问题,Unity实现虚拟摇杆可以通过以下步骤: 1. 创建一个空物体作为虚拟摇杆的父物体,命名为“Joystick”。 2. 在“Joystick”下创建两个子物体,一个是“Background”,一个是“Handle”。 3. 在“Background”下添加一个Image组件,用于显示虚拟摇杆的背景图。 4. 在“Handle”下添加一个Image组件,用于显示虚拟摇杆的手柄图。 5. 在“Joystick”上添加一个脚本,用于控制虚拟摇杆的行为。 以下是一个简单的虚拟摇杆脚本示例: ```csharp using UnityEngine; using UnityEngine.EventSystems; public class VirtualJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler { private RectTransform background; private RectTransform handle; private Vector2 inputVector; private void Start() { background = transform.GetChild(0).GetComponent<RectTransform>(); handle = transform.GetChild(1).GetComponent<RectTransform>(); } public virtual void OnDrag(PointerEventData eventData) { Vector2 pos; if (RectTransformUtility.ScreenPointToLocalPointInRectangle(background, eventData.position, eventData.pressEventCamera, out pos)) { pos.x = (pos.x / background.sizeDelta.x); pos.y = (pos.y / background.sizeDelta.y); inputVector = new Vector2(pos.x * 2 + 1, pos.y * 2 - 1); inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector; handle.anchoredPosition = new Vector2(inputVector.x * (background.sizeDelta.x / 3), inputVector.y * (background.sizeDelta.y / 3)); } } public virtual void OnPointerDown(PointerEventData eventData) { OnDrag(eventData); } public virtual void OnPointerUp(PointerEventData eventData) { inputVector = Vector2.zero; handle.anchoredPosition = Vector2.zero; } public float Horizontal() { if (inputVector.x != 0) return inputVector.x; else return Input.GetAxis("Horizontal"); } public float Vertical() { if (inputVector.y != 0) return inputVector.y; else return Input.GetAxis("Vertical"); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值