Unity虛擬搖桿的實現(xiàn)方法
更新時間:2020年04月15日 14:19:09 作者:yy763496668
這篇文章主要為大家詳細介紹了Unity虛擬搖桿的實現(xiàn)方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Unity實現(xiàn)虛擬搖桿的具體代碼,供大家參考,具體內(nèi)容如下
設(shè)置搖桿的背景圖片的錨點如下:
設(shè)置搖桿的錨點為背景圖片的中心點。
并給搖桿綁定腳本如下:
using UnityEngine; using UnityEngine.EventSystems; using System.Collections; using System; public class JoyStickController : MonoBehaviour,IDragHandler,IEndDragHandler { //最大的拖動距離 public float maxDragDistance = 50f; //虛擬搖桿的方向 public Vector3 direction; //玩家 public GameObject player; // Use this for initialization void Start() { } // Update is called once per frame void Update() { //屏幕上的y軸分量 當(dāng)作游戲世界里的z分量 //設(shè)置玩家的朝向 player.transform.forward = new Vector3(direction.x,0,direction.y); int flag = Vector3.Distance(Vector3.zero, this.transform.localPosition) <1f ? 0 : 1; player.transform.Translate(Vector3.forward * flag * Time.deltaTime,Space.Self); } //拖拽中的時候 public void OnDrag(PointerEventData eventData) { this.transform.position = Input.mousePosition; if (Vector3.Distance(Vector3.zero,this.transform.localPosition) > maxDragDistance) { direction = this.transform.position - Vector3.zero; this.transform.localPosition = direction.normalized * maxDragDistance; } } //拖拽結(jié)束的時候 public void OnEndDrag(PointerEventData eventData) { this.transform.localPosition = Vector3.zero; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實現(xiàn)字母與ASCII碼互相轉(zhuǎn)換
ASCII是基于拉丁字母的編碼系統(tǒng),也是現(xiàn)今最通用的單字節(jié)編碼系統(tǒng),本文主要為大家詳細介紹了如何使用C#實現(xiàn)字母與ASCII碼互轉(zhuǎn),需要的可以參考下2024-01-01Avalonia封裝實現(xiàn)指定組件允許拖動的工具類
這篇文章主要為大家詳細介紹了Avalonia如何封裝實現(xiàn)指定組件允許拖動的工具類,文中的示例代碼講解詳細,感興趣的小伙伴快跟隨小編一起來學(xué)習(xí)學(xué)習(xí)吧2023-03-03Unity ScrollRect實現(xiàn)軌跡滑動效果
這篇文章主要為大家詳細介紹了Unity ScrollRect實現(xiàn)軌跡滑動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09C#中的out參數(shù)、ref參數(shù)和params可變參數(shù)用法介紹
這篇文章介紹了C#中的out參數(shù)、ref參數(shù)和params可變參數(shù)用法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01C#中ManualResetEvent實現(xiàn)線程的暫停與恢復(fù)
本文主要介紹了C#中ManualResetEvent實現(xiàn)線程的暫停與恢復(fù),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01