小遊戲網站原始碼,Unity 超級簡單搖桿製作
阿新 • • 發佈:2021-08-06
小遊戲網站原始碼,Unity 超級簡單搖桿製作實現的相關程式碼
1:首先建立兩個Image,然後將其中一個為父物體,另一個為子物體,並且調整好大小
2:在父物體上寫個JoyStick.cs指令碼:
```cpp 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上面的指令碼
```cpp 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的吧
```cpp 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; } } ```
以上就是小遊戲網站原始碼,Unity 超級簡單搖桿製作實現的相關程式碼, 更多內容歡迎關注之後的文章