Unity实现简单摇杆的制作

 更新时间:2021年09月16日 15:17:29   作者:Zero_LJ  
这篇文章主要为大家详细介绍了Unity实现简单摇杆的制作,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

利用UGUI制作一个简单摇杆,效果图

1、首先建立两个Image,然后将其中一个为父物体,另一个为子物体,并且调整好大小:

ps:将子物体的锚点设置为居中          

2、在父物体上写个JoyStick.cs脚本:

  

using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
public class JoyStick : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDragHandler
{
    public static float h, v;  //传出hv
    public float maxDis;    //最大距离
 
    private RectTransform childRectTrans;
    private Coroutine coroutine = null;
 
    void Start()
    {
        childRectTrans = transform.GetChild(0) as RectTransform;
    }
    public void OnBeginDrag(PointerEventData eventData)
    {
        if (coroutine != null)
        {
            StopCoroutine(coroutine);
            coroutine = null;
        }
    }
    public void OnDrag(PointerEventData eventData)
    {
        Vector3 outPos;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(this.transform as RectTransform, eventData.position, eventData.pressEventCamera, out outPos))
        {
            childRectTrans.position = outPos;
 
            //限制拖拽距离
            childRectTrans.anchoredPosition = Vector2.ClampMagnitude(childRectTrans.anchoredPosition, maxDis);
 
            //或者利用子物体和父物体的距离判断是否超过最大距离,当距离大于等于最大的距离时候,
            //计算父物体和子物体的向量,然后利用向量*最大距离来限制拖拽距离
            //if (Vector2.Distance(childRectTrans.position, this.transform.position) > maxDis)
            //{
            //    Vector2 dir = (childRectTrans.position - this.transform.position).normalized;
            //    childRectTrans.anchoredPosition = dir * maxDis;
            //}
            GetHV();
        }
    }
 
    public void OnEndDrag(PointerEventData eventData)
    {
        //当结束拖动,要将物体归0,为了加一点缓冲效果
        //(1)可以使用dotween等补间动画插件,会减少很多
        //rectTransform.DoAnchoredPos(Vector2.zero,0.5f);
        //(2)或者使用携程 这里使用携程
        if (coroutine == null)
            coroutine = StartCoroutine(IEToZeroPos(childRectTrans, 0.1f));
    }
    private void GetHV()
    {
        h = childRectTrans.anchoredPosition.x / maxDis;
        v = childRectTrans.anchoredPosition.y / maxDis;
    }
    private IEnumerator IEToZeroPos(RectTransform rectTransform, float duartion)
    {
        if (duartion == 0f)
        {
            yield return null;
            rectTransform.anchoredPosition = Vector2.zero;
            GetHV();
            coroutine = null;
            yield break;
        }
        Vector2 currentpos = rectTransform.anchoredPosition;
        float offx = currentpos.x / duartion;
        float offy = currentpos.y / duartion;
        while (rectTransform.anchoredPosition != Vector2.zero)
        {
            yield return null;
            rectTransform.anchoredPosition = new Vector2(rectTransform.anchoredPosition.x - offx * Time.deltaTime, rectTransform.anchoredPosition.y - offy * Time.deltaTime);
            GetHV();
            if (rectTransform.anchoredPosition.sqrMagnitude < 8f)
            {
                rectTransform.anchoredPosition = Vector2.zero;
                GetHV();
                coroutine = null;
                break;
            }
        }
    }
}

另外附上Cube上面的脚本  

private void Update()
    {
        Vector3 dir = new Vector3(JoyStick.h, 0, JoyStick.v);
        if (dir.sqrMagnitude > 0)
        {
            transform.Translate(dir * 3f * Time.deltaTime,Space.World);
            Quaternion targatRotate = Quaternion.LookRotation(dir, Vector3.up);
            transform.rotation = Quaternion.Slerp(transform.rotation, targatRotate, 3 * Time.deltaTime);
        }
    }

加个使用doTween的吧

using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections; using DG.Tweening;
public class JoyStick : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDragHandler
{
    public static float h, v;  //传出hv
    public float maxDis;    //最大距离
 
    private RectTransform childRectTrans;
    private Coroutine coroutine = null;
 
    void Start()
    {
        childRectTrans = transform.GetChild(0) as RectTransform;
    }
    public void OnBeginDrag(PointerEventData eventData)
    {
        if (coroutine != null)
        {
            StopCoroutine(coroutine);
            coroutine = null;
        }
    }
    public void OnDrag(PointerEventData eventData)
    {
        Vector3 outPos;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(this.transform as RectTransform, eventData.position, eventData.pressEventCamera, out outPos))
        {
            childRectTrans.position = outPos;
 
            //限制拖拽距离
            childRectTrans.anchoredPosition = Vector2.ClampMagnitude(childRectTrans.anchoredPosition, maxDis);
 
            //或者利用子物体和父物体的距离判断是否超过最大距离,当距离大于等于最大的距离时候,
            //计算父物体和子物体的向量,然后利用向量*最大距离来限制拖拽距离
            //if (Vector2.Distance(childRectTrans.position, this.transform.position) > maxDis)
            //{
            //    Vector2 dir = (childRectTrans.position - this.transform.position).normalized;
            //    childRectTrans.anchoredPosition = dir * maxDis;
            //}
            GetHV();
        }
    }
 
    public void OnEndDrag(PointerEventData eventData)
    {
        //当结束拖动,要将物体归0,为了加一点缓冲效果
        //(1)可以使用dotween等补间动画插件,会减少很多
        rectTransform.DoAnchoredPos(Vector2.zero,0.5f).OnUpdate(GetHV);     
    }
    private void GetHV()
    {
        h = childRectTrans.anchoredPosition.x / maxDis;
        v = childRectTrans.anchoredPosition.y / maxDis;
    }
  
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 同时兼容JS和C#的RSA加密解密算法详解(对web提交的数据加密传输)

    同时兼容JS和C#的RSA加密解密算法详解(对web提交的数据加密传输)

    这篇文章主要给大家介绍了关于同时兼容JS和C#的RSA加密解密算法,通过该算法可以对web提交的数据进行加密传输,文中通过图文及示例代码介绍的非常详细,需要的朋友们可以参考借鉴,下面来一起看看吧。
    2017-07-07
  • ZooKeeper的安装及部署教程

    ZooKeeper的安装及部署教程

    Zookeeper是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护、名字服务、分布式同步、组服务等,这篇文章主要介绍了ZooKeeper的安装及部署,需要的朋友可以参考下
    2019-06-06
  • C#编程之事务用法

    C#编程之事务用法

    这篇文章主要介绍了C#编程之事务用法,结合实例形式对比分析了C#中事务提交与回滚的具体实现技巧与相关注意事项,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • 二叉树的遍历算法(详细示例分析)

    二叉树的遍历算法(详细示例分析)

    以下代码是对二叉树的遍历算法进行了分析介绍,需要的朋友可以参考下
    2013-05-05
  • C#使用NPOI对Excel数据进行导入导出

    C#使用NPOI对Excel数据进行导入导出

    这篇文章介绍了C#使用NPOI对Excel数据进行导入导出的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • WPF实现雷达图(仿英雄联盟)的示例代码

    WPF实现雷达图(仿英雄联盟)的示例代码

    这篇文章主要介绍了如何利用WPF实现雷达图(仿英雄联盟)的绘制,文中的示例代码讲解详细,对我们学习或工作有一定帮助,需要的可以参考一下
    2022-07-07
  • C#调用sql2000存储过程方法小结

    C#调用sql2000存储过程方法小结

    这篇文章主要介绍了C#调用sql2000存储过程的方法,以实例形式分别对调用带输入参数及输出参数的存储过程进行了详细分析,非常具有实用价值,需要的朋友可以参考下
    2014-10-10
  • 剖析设计模式编程中C#对于组合模式的运用

    剖析设计模式编程中C#对于组合模式的运用

    这篇文章主要介绍了设计模式编程中C#对于组合模式的运用,理论上来说组合模式包含抽象构件、树叶构件和树枝构件三个角色,需要的朋友可以参考下
    2016-02-02
  • 详解C#编程中一维数组与多维数组的使用

    详解C#编程中一维数组与多维数组的使用

    这篇文章主要介绍了详解C#编程中一维数组与多维数组的使用,包括数组初始化等基础知识的讲解,需要的朋友可以参考下
    2016-01-01
  • C#采用OpenXml实现给word文档添加文字

    C#采用OpenXml实现给word文档添加文字

    这篇文章主要介绍了C#采用OpenXml实现给word文档添加文字的方法,包括了用法的实例分析,是非常实用的技巧,需要的朋友可以参考下
    2014-09-09

最新评论