您好,登錄后才能下訂單哦!
unity3d教程:雙搖桿設(shè)計(jì)思路
using UnityEngine; using System.Collections; public enum JoyStickType { leftJoyStick, rightJoyStick } public class JoyStick : MonoBehaviour { public JoyStickType joyStickType;//搖桿類(lèi)型,左搖桿還是右搖桿 public Vector2 centerPos;//搖桿的中心點(diǎn)位置,世界坐標(biāo) public Transform centerBall;//搖桿中心球 public float joyStickRadius;//搖桿的半徑,世界空間中的半徑 private int lastFingerID = -1;//最后一次觸摸的手指id private bool centerBallMoving = false;//搖桿中心球移動(dòng)開(kāi)關(guān) // Use this for initialization void Start () { } // Update is called once per frame void Update () { JoyStickController(); } void JoyStickController() { int count = Input.touchCount;//獲取觸摸數(shù)量 for (int i = 0; i < count; i++)//逐個(gè)分析觸摸 { Touch touch = Input.GetTouch(i);Unity3D教程手冊(cè) //將當(dāng)前的觸摸位置屏幕轉(zhuǎn)世界坐標(biāo) Vector2 currentTouchPos = new Vector2(Camera.main.ScreenToWorldPoint(touch.position).x, Camera.main.ScreenToWorldPoint(touch.position).y); Vector2 temp = currentTouchPos - centerPos;//得到方向向量temp(觸摸轉(zhuǎn)換后的位置和搖桿) if (touch.phase == TouchPhase.Began) { if (Mathf.Round(temp.magnitude) <= joyStickRadius)//如果方向向量temp的長(zhǎng)度沒(méi)有超出搖桿的半徑 { lastFingerID = touch.fingerId;//記錄該觸摸的id centerBallMoving = true;//搖桿中心球移動(dòng)開(kāi)關(guān)打開(kāi) } } //若中心球移動(dòng)開(kāi)關(guān)打開(kāi),搖桿中心球就會(huì)跟隨手指移動(dòng)。但需要加以限制,當(dāng)手指觸摸沒(méi)有超出搖桿的圓形區(qū)域時(shí),中心球完全跟隨手指觸摸; //當(dāng)手指觸摸超出圓形區(qū)域時(shí),中心球處于觸摸位置和搖桿中心點(diǎn)所形成的方向上并且不能超出半徑 if (touch.fingerId == lastFingerID && centerBallMoving) { if (Mathf.Round(temp.magnitude) <= joyStickRadius) //如果手指觸摸沒(méi)有超出搖桿的圓形區(qū)域,即搖桿半徑,搖桿中心球的位置一直跟隨手指 { centerBall.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, Camera.main.WorldToScreenPoint(centerBall.transform.position).z)); } else { temp.Normalize(); //由于搖桿中心球是在一個(gè)平面上移動(dòng)的,本人做法是中心球位置的z值固定為18,改變x,y值,大家可以視情況做一些修改 centerBall.transform.position = new Vector3((temp * joyStickRadius + centerPos).x, (temp * joyStickRadius + centerPos).y, 18); } if (temp.x >= 0)//0-180 { //一下為示例代碼:控制旋轉(zhuǎn)方向,主要利用Vector2.Angle(temp, new Vector2(0, 5))得到角度并利用 //initialization_script.current_player_tank_script.BodyRotation(Vector2.Angle(temp, new Vector2(0, 5))); } if (temp.x< 0)//180-360 { //一下為示例代碼:控制旋轉(zhuǎn)方向,主要利用Vector2.Angle(temp, new Vector2(0, 5))得到角度并利用 //initialization_script.current_player_tank_script.BodyRotation(-1 * Vector2.Angle(temp1, new Vector2(0, 5))); } //控制移動(dòng)的函數(shù)或者控制開(kāi)火的函數(shù),假設(shè)左搖桿控制移動(dòng),右搖桿控制開(kāi)火 switch(joyStickType) { case JoyStickType.leftJoyStick: //Move(); break; case JoyStickType.rightJoyStick: //Fire() break; } //當(dāng)釋放觸摸的時(shí)候中心球位置重置 if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) { centerBall.transform.position = new Vector3(centerPos.x, centerPos.y, 18); // 示例代碼,視情況自己添加 initialization_script.current_player_tank_script.CoverRotation(initialization_script.current_player_tank.transform.eulerAngles.y); centerBallMoving = false; lastFingerID = -1; } } } } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。