Unity实现简单摇杆的制作_第1页
Unity实现简单摇杆的制作_第2页
Unity实现简单摇杆的制作_第3页
Unity实现简单摇杆的制作_第4页
Unity实现简单摇杆的制作_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第Unity实现简单摇杆的制作利用UGUI制作一个简单摇杆,效果图

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

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

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

usingUnityEngine;

usingUnityEngine.EventSystems;

usingSystem.Collections;

publicclassJoyStick:MonoBehaviour,IDragHandler,IEndDragHandler,IBeginDragHandler

publicstaticfloath,v;//传出hv

publicfloatmaxDis;//最大距离

privateRectTransformchildRectTrans;

privateCoroutinecoroutine=null;

voidStart()

childRectTrans=transform.GetChild(0)asRectTransform;

publicvoidOnBeginDrag(PointerEventDataeventData)

if(coroutine!=null)

StopCoroutine(coroutine);

coroutine=null;

publicvoidOnDrag(PointerEventDataeventData)

Vector3outPos;

if(RectTransformUtility.ScreenPointToWorldPointInRectangle(this.transformasRectTransform,eventData.position,eventData.pressEventCamera,outoutPos))

childRectTrans.position=outPos;

//限制拖拽距离

childRectTrans.anchoredPosition=Vector2.ClampMagnitude(childRectTrans.anchoredPosition,maxDis);

//或者利用子物体和父物体的距离判断是否超过最大距离,当距离大于等于最大的距离时候,

//计算父物体和子物体的向量,然后利用向量*最大距离来限制拖拽距离

//if(Vector2.Distance(childRectTrans.position,this.transform.position)maxDis)

//Vector2dir=(childRectTrans.position-this.transform.position).normalized;

//childRectTrans.anchoredPosition=dir*maxDis;

GetHV();

publicvoidOnEndDrag(PointerEventDataeventData)

//当结束拖动,要将物体归0,为了加一点缓冲效果

//(1)可以使用dotween等补间动画插件,会减少很多

//rectTransform.DoAnchoredPos(Vector2.zero,0.5f);

//(2)或者使用携程这里使用携程

if(coroutine==null)

coroutine=StartCoroutine(IEToZeroPos(childRectTrans,0.1f));

privatevoidGetHV()

h=childRectTrans.anchoredPosition.x/maxDis;

v=childRectTrans.anchoredPosition.y/maxDis;

privateIEnumeratorIEToZeroPos(RectTransformrectTransform,floatduartion)

if(duartion==0f)

yieldreturnnull;

rectTransform.anchoredPosition=Vector2.zero;

GetHV();

coroutine=null;

yieldbreak;

Vector2currentpos=rectTransform.anchoredPosition;

floatoffx=currentpos.x/duartion;

floatoffy=currentpos.y/duartion;

while(rectTransform.anchoredPosition!=Vector2.zero)

yieldreturnnull;

rectTransform.anchoredPosition=newVector2(rectTransform.anchoredPosition.x-offx*Time.deltaTime,rectTransform.anchoredPosition.y-offy*Time.deltaTime);

GetHV();

if(rectTransform.anchoredPosition.sqrMagnitude8f)

rectTransform.anchoredPosition=Vector2.zero;

GetHV();

coroutine=null;

break;

}

另外附上Cube上面的脚本

privatevoidUpdate()

Vector3dir=newVector3(JoyStick.h,0,JoyStick.v);

if(dir.sqrMagnitude0)

transform.Translate(dir*3f*Time.deltaTime,Space.World);

QuaterniontargatRotate=Quaternion.LookRotation(dir,Vector3.up);

transform.rotation=Quaternion.Slerp(transform.rotation,targatRotate,3*Time.deltaTime);

}

加个使用doTween的吧

usingUnityEngine;

usingUnityEngine.EventSystems;

usingSystem.Collections;usingDG.Tweening;

publicclassJoyStick:MonoBehaviour,IDragHandler,IEndDragHandler,IBeginDragHandler

publicstaticfloath,v;//传出hv

publicfloatmaxDis;//最大距离

privateRectTransformchildRectTrans;

privateCoroutinecoroutine=null;

voidStart()

childRectTrans=transform.GetChild(0)asRectTransform;

publicvoidOnBeginDrag(PointerEventDataeventData)

if(coroutine!=null)

StopCoroutine(coroutine);

coroutine=null;

publicvoidOnDrag(PointerEventDataeventData)

Vector3outPos;

if(RectTransformUtility.ScreenPointToWorldPointInRectangle(this.transformasRectTransform,eventData.position,eventData.pressEventCamera,outoutPos))

childRectTrans.position=outPos;

//限制拖拽距离

childRectTrans.anchoredPosition=Vector2.ClampMagnitude(childRectTrans.anchoredPosition,maxDis);

//或者利用子物体和父物体的距离判断是否超过最大距离,当距离大于等于最大的距离时候,

//计算父物体和子物体的向量,然后利用向量*最大距离来限制拖拽距离

//if(Vector2.Distance(childRectTrans.position,this.transform.position)maxDis)

//Vector2dir=(childRectTrans.position-this.transform.position).normalized;

//childRectTrans.anchoredPosition=dir*maxDis;

GetHV();

publicvoidOnEndDrag(PointerEventDataeventData)

//当结束拖动,要将物体归0,为了加一点缓冲效果

//(1)可以使用dotween等补间动画插件,会减少很多

rectTran

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论