Unity虛擬搖桿的實(shí)現(xiàn)方法
本文實(shí)例為大家分享了Unity實(shí)現(xiàn)虛擬搖桿的具體代碼,供大家參考,具體內(nèi)容如下
設(shè)置搖桿的背景圖片的錨點(diǎn)如下:
設(shè)置搖桿的錨點(diǎn)為背景圖片的中心點(diǎn)。
并給搖桿綁定腳本如下:
using UnityEngine; using UnityEngine.EventSystems; using System.Collections; using System; public class JoyStickController : MonoBehaviour,IDragHandler,IEndDragHandler { //最大的拖動(dòng)距離 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); } //拖拽中的時(shí)候 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é)束的時(shí)候 public void OnEndDrag(PointerEventData eventData) { this.transform.localPosition = Vector3.zero; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- unity實(shí)現(xiàn)手機(jī)端搖桿控制人物移動(dòng)
- Unity實(shí)現(xiàn)簡(jiǎn)單的虛擬搖桿
- unity實(shí)現(xiàn)虛擬搖桿控制Virtual Joystick
- Unity UGUI通過(guò)搖桿控制角色移動(dòng)
- unity實(shí)現(xiàn)手游虛擬搖桿
- Unity3D基于UGUI實(shí)現(xiàn)虛擬搖桿
- Unity3D使用UGUI開(kāi)發(fā)原生虛擬搖桿
- Unity實(shí)現(xiàn)簡(jiǎn)單虛擬搖桿
- Unity使用ScrollRect制作搖桿
- Unity實(shí)現(xiàn)簡(jiǎn)單搖桿的制作
相關(guān)文章
C#實(shí)現(xiàn)字母與ASCII碼互相轉(zhuǎn)換
ASCII是基于拉丁字母的編碼系統(tǒng),也是現(xiàn)今最通用的單字節(jié)編碼系統(tǒng),本文主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)字母與ASCII碼互轉(zhuǎn),需要的可以參考下2024-01-01Avalonia封裝實(shí)現(xiàn)指定組件允許拖動(dòng)的工具類(lèi)
這篇文章主要為大家詳細(xì)介紹了Avalonia如何封裝實(shí)現(xiàn)指定組件允許拖動(dòng)的工具類(lèi),文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧2023-03-03Unity ScrollRect實(shí)現(xiàn)軌跡滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Unity ScrollRect實(shí)現(xiàn)軌跡滑動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09C#中的out參數(shù)、ref參數(shù)和params可變參數(shù)用法介紹
這篇文章介紹了C#中的out參數(shù)、ref參數(shù)和params可變參數(shù)用法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01C#中ManualResetEvent實(shí)現(xiàn)線(xiàn)程的暫停與恢復(fù)
本文主要介紹了C#中ManualResetEvent實(shí)現(xiàn)線(xiàn)程的暫停與恢復(fù),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01