Unity_虚拟摇杆的实现_065
2017-08-26 19:42
489 查看
设置摇杆的背景图片的锚点如下:
设置摇杆的锚点为背景图片的中心点。
并给摇杆绑定脚本如下:
设置摇杆的锚点为背景图片的中心点。
并给摇杆绑定脚本如下:
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轴分量 当作游戏世界里的z分量 //设置玩家的朝向 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; } } //拖拽结束的时候 public void OnEndDrag(PointerEventData eventData) { this.transform.localPosition = Vector3.zero; } }
相关文章推荐
- Unity实现虚拟摇杆
- Unity2D - 4. 实现android虚拟摇杆控制人物移动
- UGUI实现Unity虚拟摇杆
- Unity手游之路<五>虚拟摇杆之Unity内置插件实现
- unity虚拟摇杆的实现
- Unity手游之路<五>虚拟摇杆之Unity内置插件实现
- Unity手游之路<五>虚拟摇杆之Unity内置插件实现
- unity中虚拟摇杆的实现
- Unity中虚拟摇杆的简单实现
- Unity手游之路<五>虚拟摇杆之Unity内置插件实现
- Unity 虚拟摇杆之Unity内置插件实现
- Cocos2d-x3.2实现虚拟摇杆多点触摸
- 基于Unity3D的虚拟摇杆的实现
- Unity用UGUI做虚拟摇杆
- quick-lua 实现 虚拟摇杆
- Cocos2d-x3.2实现虚拟摇杆多点触摸
- UGUI实现unity摇杆
- unity 手游虚拟摇杆代码参考
- 基于Cocos2d-x3.2的虚拟摇杆实现及操控角色移动