UGUI实现摇杆
2015-06-29 20:34 糯米粥 阅读(1565) 评论(0) 编辑 收藏 举报效果图
立方体好比角色。可以利用右下角的摇杆控制角色移动
看看结构图
Image1:是外面圆圈
Image2:是蓝色圈
创建JoyStickScript.cs代码挂载在Image2上。这里没有实现控制立方体移动。有待改进
1 using UnityEngine; 2 using System.Collections; 3 using UnityEngine.UI; 4 using UnityEngine.EventSystems; 5 6 /// <summary> 7 /// 摇杆 8 /// </summary> 9 public class JoyStickScript : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler 10 { 11 12 GameObject Player; //移动的对象 13 14 Vector3 originPosition;//起点坐标 15 Vector3 newPosition; 16 public float Radius = 40; //滚动的最大半径 17 float horizontal; 18 float vertical; 19 float speed; 20 void Awake() 21 { 22 Player = GameObject.FindWithTag("Player"); 23 } 24 25 // Use this for initialization 26 void Start() 27 { 28 //originPosition = transform.position;//记录原点位置 29 } 30 31 // Update is called once per frame 32 void Update() 33 { 34 //float horizontal = Input.GetAxis("Horizontal"); 35 //float veritcal = (transform.position.y - originPosition.y) / Radius; 36 //vertical = (transform.position.y - originPosition.y) / Radius; 37 //horizontal = (transform.position.x - originPosition.x) / Radius; 38 //Player.transform.Translate(horizontal, vertical, 0); 39 //Debug.Log(vertical); 40 41 } 42 43 public void OnEndDrag(PointerEventData eventData) 44 { 45 transform.position = originPosition; //归位到原点 46 } 47 48 Vector3 dir; 49 public void OnDrag(PointerEventData eventData) 50 { 51 52 dir = (Input.mousePosition - originPosition).normalized; 53 54 transform.position = Input.mousePosition; //设置当前摇杆位置 55 56 //当超过了外面的圆圈 57 if (Vector3.Distance(Input.mousePosition, originPosition) > Radius) 58 transform.position = originPosition + dir * Radius; 59 60 //vertical = (transform.position.y - originPosition.y) / Radius; 61 //horizontal = (transform.position.x - originPosition.x) / Radius; 62 print(originPosition + dir * Radius); 63 Player.transform.position = dir * Time.deltaTime; 64 } 65 66 public void OnBeginDrag(PointerEventData eventData) 67 { 68 originPosition = transform.position;//记录原点位置 69 } 70 }
-------------------分割线 实现摇杆控制角色移动---------------------------------------
joystack是空对象
backgound是背景
JoystackControl是中间滚动虚拟轴
JoystackCc.cs代码挂载在JoystackControl上
1 using UnityEngine; 2 using System.Collections; 3 using UnityEngine.EventSystems; 4 /// <summary> 5 /// 这是主要来控制摇杆的。 6 /// </summary> 7 public class JoystackCc : MonoBehaviour 8 , IBeginDragHandler, IDragHandler, IEndDragHandler 9 { 10 11 public Vector3 originPosition;//起点坐标 12 public float speed = 2f;//角色移动速度 13 public float Radius = 40; //滚动的最大半径 14 Vector3 dir; //滚动的方向 15 16 17 public Vector3 MovePosiNorm; //标准化移动的距离 18 // Use this for initialization 19 void Start() 20 { 21 22 } 23 24 // Update is called once per frame 25 void Update() 26 { 27 28 } 29 30 public void OnBeginDrag(PointerEventData eventData) 31 { 32 originPosition = transform.position; 33 } 34 35 public void OnDrag(PointerEventData eventData) 36 { 37 //获取当前鼠标或者触摸算出拖动距离 38 dir = (Input.mousePosition - originPosition).normalized; 39 40 //拖到滚轮跟着鼠标走 41 transform.position = Input.mousePosition; 42 43 44 //originPosition = transform.position; 45 46 //如果滚轮范围超过了外面的大圆 即大于可拖动距离 47 if (Vector3.Distance(originPosition, Input.mousePosition) > Radius) 48 { 49 transform.position = originPosition + dir * Radius; 50 } 51 52 if (Vector3.Distance(transform.position, originPosition) > 1) //距离大于激活移动的距离 53 { 54 MovePosiNorm = (transform.position - originPosition).normalized; 55 MovePosiNorm = new Vector3(MovePosiNorm.x, 0, MovePosiNorm.y); 56 } 57 else 58 MovePosiNorm = Vector3.zero; 59 60 61 PlayerMoveControl.moveStart(); 62 } 63 64 public void OnEndDrag(PointerEventData eventData) 65 { 66 transform.position = originPosition; 67 PlayerMoveControl.moveEnd(); 68 } 69 }
PlayerMoveControl.cs代码挂载在角色上
1 using UnityEngine; 2 using System.Collections; 3 4 /// <summary> 5 /// 控制玩家移动 6 /// </summary> 7 public class PlayerMoveControl : MonoBehaviour 8 { 9 10 private Transform _mTransform; 11 public JoystackCc _mJoystackCc; 12 13 public delegate void MoveDelegate(); 14 public static PlayerMoveControl Instance; 15 public static MoveDelegate moveStart; 16 public static MoveDelegate moveEnd; 17 18 private float angle; 19 private bool _turnBase = false; 20 void Awake() 21 { 22 Instance = this; 23 24 _mTransform = transform; 25 moveStart = OnMoveStart; 26 moveEnd = OnMoveEnd; 27 } 28 29 void OnMoveStart() 30 { 31 _turnBase = true; 32 } 33 void OnMoveEnd() 34 { 35 _turnBase = false; 36 } 37 38 39 // Use this for initialization 40 void Start() 41 { 42 43 } 44 45 // Update is called once per frame 46 void Update() 47 { 48 if (_turnBase) 49 { 50 Vector3 vecMove = _mJoystackCc.MovePosiNorm * Time.deltaTime; 51 52 _mTransform.position += vecMove; 53 54 //把弧度转为角度 55 angle = Mathf.Atan2(_mJoystackCc.MovePosiNorm.x, _mJoystackCc.MovePosiNorm.z) * Mathf.Rad2Deg; 56 57 transform.rotation = Quaternion.Euler(Vector3.up * angle); 58 59 } 60 } 61 }
为了让相机跟随角色,
写个脚本挂载在相机上
1 using UnityEngine; 2 using System.Collections; 3 4 public class yid : MonoBehaviour 5 { 6 7 Vector3 offset; //角色与相机之间的偏移量 8 public Transform target; //需要跟踪的角色 9 10 // Use this for initialization 11 void Start() 12 { 13 //offset = transform.position - target.position; 14 15 offset = target.position - transform.position; 16 } 17 18 // Update is called once per frame 19 void Update() 20 { 21 transform.position = target.position - offset; 22 } 23 }