Unity實(shí)現(xiàn)簡(jiǎn)單的虛擬搖桿
本文實(shí)例為大家分享了Unity實(shí)現(xiàn)簡(jiǎn)單虛擬搖桿的具體代碼,供大家參考,具體內(nèi)容如下
需求:點(diǎn)擊創(chuàng)建一個(gè)虛擬搖桿底盤(pán),鼠標(biāo)拖拽時(shí)候上方搖桿會(huì)跟隨鼠標(biāo)方向移動(dòng),并且不會(huì)超出搖桿盤(pán)范圍
*搖桿功能另外實(shí)現(xiàn)
UI顯示
using System.Collections; using System.Collections.Generic; using UnityEngine; public class RockingIcon : MonoBehaviour { public Transform touchPoint; public Transform bgPoint; public float radius; bool isPressing; Vector3 bgPos; private void Update() { bool pressing; Vector3 pos; if (Application.isEditor) GetPressingInfoInEditor(out pressing, out pos); else GetPressingInfoInPhone(out pressing, out pos); SetIcon(pressing, pos); } void GetPressingInfoInEditor(out bool pressing, out Vector3 pos) { if (Input.GetMouseButton(0)) { pressing = true; pos = Input.mousePosition; } else { pressing = false; pos = Vector3.zero; } } void GetPressingInfoInPhone(out bool pressing, out Vector3 pos) { if(Input.touchCount > 0) { pressing = true; pos = Input.GetTouch(0).position; } else { pressing = false; pos = Vector3.zero; } } void SetIcon(bool pressing, Vector3 pos) { if (pressing) { if (!isPressing) { bgPoint.gameObject.SetActive(true); bgPoint.transform.position = pos; bgPos = pos; isPressing = true; } else { bgPoint.gameObject.SetActive(true); SetTouchPointPos(pos); } } else { touchPoint.gameObject.SetActive(false); bgPoint.gameObject.SetActive(false); isPressing = false; } } void SetTouchPointPos(Vector3 pos) { Vector3 center = bgPoint.position; Vector3 touch = pos; Vector3 to; float distance = Vector3.Distance(center, touch); if (distance < radius) to = touch; else { Vector3 dir = touch - center; dir.Normalize(); to = dir * radius; to += center; } touchPoint.gameObject.SetActive(true); touchPoint.transform.position = to; } }
預(yù)制:
操作控制
#region 鼠標(biāo)操作 float min_move_x = Global.min_move_distance * (Screen.width / 1080f); float min_move_y = Global.min_move_distance * (Screen.height / 1900f); if(Application.platform == RuntimePlatform.WindowsEditor) { if (Input.GetMouseButtonDown(0)) { touch_time = 0; first_touch_pos = Input.mousePosition; } else if (Input.GetMouseButton(0)) { touch_time += Time.deltaTime; if (touch_time >= Global.touch_time_limit) { Vector2 touch_pos = Input.mousePosition; Vector2 distance = touch_pos - first_touch_pos; //Vector2 touch_pos_in_func = PosInTheFunc(touch_pos); //Vector2 first_pos_in_func = PosInTheFunc(first_touch_pos); //Vector2 distance = touch_pos_in_func - first_pos_in_func; if (Mathf.Abs(distance.x) > min_move_x && Mathf.Abs(distance.x) > Mathf.Abs(distance.y)) Move(distance.x > 0 ? Vector3.right : Vector3.left); if (Mathf.Abs(distance.y) > min_move_y && Mathf.Abs(distance.y) > Mathf.Abs(distance.x)) Move(distance.y > 0 ? Vector3.forward : Vector3.back); } } else if (Input.GetMouseButtonUp(0)) { //if(touch_time < Global.touch_time_limit) //{ // PutBoomb(); //} touch_time = 0; first_touch_pos = Vector3.zero; } } #endregion #region 手機(jī)操作 if (Application.platform == RuntimePlatform.Android) { if (Input.touchCount > 0) { Touch touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began) { first_touch_pos = touch.position; } else if (touch.phase == TouchPhase.Ended) { first_touch_pos = Vector3.zero; } else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary) { Vector2 touch_pos = touch.position; Vector2 distance = touch_pos - first_touch_pos; if (Mathf.Abs(distance.x) > min_move_x && Mathf.Abs(distance.x) > Mathf.Abs(distance.y)) Move(distance.x > 0 ? Vector3.right : Vector3.left); if (Mathf.Abs(distance.y) > min_move_y && Mathf.Abs(distance.y) > Mathf.Abs(distance.x)) Move(distance.y > 0 ? Vector3.forward : Vector3.back); } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- unity實(shí)現(xiàn)手機(jī)端搖桿控制人物移動(dòng)
- unity實(shí)現(xiàn)虛擬搖桿控制Virtual Joystick
- Unity UGUI通過(guò)搖桿控制角色移動(dòng)
- Unity虛擬搖桿的實(shí)現(xiàn)方法
- 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#過(guò)濾DataTable中空數(shù)據(jù)和重復(fù)數(shù)據(jù)的示例代碼
這篇文章主要給大家介紹了關(guān)于C#過(guò)濾DataTable中空數(shù)據(jù)和重復(fù)數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01C#使用Fleck實(shí)現(xiàn)創(chuàng)建WebSocket服務(wù)器
這篇文章主要為大家詳細(xì)介紹了C#如何使用Fleck實(shí)現(xiàn)創(chuàng)建WebSocket服務(wù)器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01C#實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出到word或者Excel中的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出到word或者Excel中的方法,涉及C#操作word及Excel格式文件的方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08在C#中List集合使用First()方法獲取第一個(gè)元素的操作
這篇文章主要介紹了在C#中List集合使用First()方法獲取第一個(gè)元素的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12用.NET創(chuàng)建Windows服務(wù)的方法
用.NET創(chuàng)建Windows服務(wù)的方法...2007-03-03