【Unity2022】Unity实现手机游戏操控摇杆(实现操控轮盘)

运行效果预览

首先展示一下本文章实现的效果:
在这里插入图片描述

创建物体

创建两个UI图像,一个用于表示背景,作为父物体,命名为JoyStick,一个表示摇杆,命名为Center。
在这里插入图片描述
背景图像选择一个圆,最好加点半透明的属性,最终完成图如下图所示。
在这里插入图片描述

脚本

创建一个脚本,命名为Joystick,然后将脚本挂载到JoyStick物体上,我们将通过这个脚本来实现摇杆。

获取RectTransform

我们首先需要在Awake() 方法中,获取摇杆背景的 RectTransform 组件和手柄的RectTransfomr组件。

background = GetComponent<RectTransform>();
handle = transform.GetChild(0).GetComponent<RectTransform>(); // 摇杆手柄是背景的子元素

background 变量保存了背景的 RectTransform 组件,handle 变量保存了摇杆手柄的 RectTransform 组件。

处理玩家拖动事件

接下来,编写一个方法,用于处理拖动事件,代码如下所示。

public void OnDrag(PointerEventData eventData)
{
    Vector2 touchPosition = Vector2.zero;
    if (RectTransformUtility.ScreenPointToLocalPointInRectangle(background, eventData.position, eventData.pressEventCamera, out touchPosition))
    {
        // 获取触摸位置相对于摇杆背景的百分比
        touchPosition.x = (touchPosition.x / background.sizeDelta.x)*2;
        touchPosition.y = (touchPosition.y / background.sizeDelta.y)*2;
        touchPosition = (touchPosition.magnitude > 1f) ? touchPosition.normalized : touchPosition;
        
        // 更新摇杆手柄的位置
        handle.anchoredPosition = new Vector2(touchPosition.x * (background.sizeDelta.x / 2), touchPosition.y * (background.sizeDelta.y / 2));
        // 更新输入方向
        inputDirection = touchPosition;
    }
}

OnDrag(PointerEventData eventData) 方法用于处理拖动事件。在该方法中,首先通过 RectTransformUtility.ScreenPointToLocalPointInRectangle() 方法将触摸位置从屏幕坐标系转换为摇杆背景的本地坐标系。然后将触摸位置转换成相对于摇杆背景的百分比,这样摇杆手柄就能够根据触摸位置的百分比调整自己的位置。最后更新输入方向变量。

完整代码

using UnityEngine;
using UnityEngine.EventSystems;

public class Joystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
    private RectTransform background; // 摇杆背景
    private RectTransform handle; // 摇杆手柄

    private Vector2 inputDirection; // 输入方向

    private void Awake()
    {
        background = GetComponent<RectTransform>();
        handle = transform.GetChild(0).GetComponent<RectTransform>(); // 摇杆手柄是背景的子元素
    }

    public void OnDrag(PointerEventData eventData)
    {
        Vector2 touchPosition = Vector2.zero;
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(background, eventData.position, eventData.pressEventCamera, out touchPosition))
        {
            // 获取触摸位置相对于摇杆背景的百分比
            touchPosition.x = (touchPosition.x / background.sizeDelta.x)*2;
            touchPosition.y = (touchPosition.y / background.sizeDelta.y)*2;
            touchPosition = (touchPosition.magnitude > 1f) ? touchPosition.normalized : touchPosition;
            
            // 更新摇杆手柄的位置
            handle.anchoredPosition = new Vector2(touchPosition.x * (background.sizeDelta.x / 2), touchPosition.y * (background.sizeDelta.y / 2));
            // 更新输入方向
            inputDirection = touchPosition;
        }
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        OnDrag(eventData);
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        // 重置摇杆位置和输入方向
        handle.anchoredPosition = Vector2.zero;
        inputDirection = Vector2.zero;
    }

    // 返回输入方向
    public Vector2 GetInputDirection()
    {
        return inputDirection;
    }
}

获取输入

接下来就可以在其他所需获取输入的地方调用我们的GetInputDirection方法,来获取玩家的输入了,具体示例代码如下所示;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : Ball
{
    public Joystick joystick;
    // Start is called before the first frame update
    protected override void Start()
    {
        base.Start();
        GameObject canvas = GameObject.FindGameObjectWithTag("UI");
        joystick = canvas.transform.Find("Input/JoyStick").GetComponent<Joystick>();
    }
// Update is called once per frame
void Update()
    {
        Vector2 joystickInput=joystick.GetInputDirection();
        Move(new Vector3(joystickInput.x,joystickInput.y,transform.position.z));
        //Debug.Log(Weight);
    }
}

运行

运行程序,效果如下所示:
在这里插入图片描述

其他文章

Unity实现在两个物体之间连出一条线

Unity如何生成随机数

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水智

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值