最近初学Easytouch,感觉很好用,网上资料也很多,至于摇杆控制移动问题,网上有很多,我在这里有也是记录一下,好记性不如烂笔头嘛
至于EasyTouch其它的问题我就多不说了,网上很多,直接上代码吧: EasyTouch摇杆控制3D物体移动: 挂在3D物体上。
using UnityEngine; using System.Collections; public class PlayerController : MonoBehaviour { public EasyJoystick joystick;//虚拟摇杆 private float speed = 2.0f; void OnEnable() { //注册事件 EasyJoystick.On_JoystickMoveStart += JoystickMoveStart; EasyJoystick.On_JoystickMove += JoystickHandlerMoving; EasyJoystick.On_JoystickMoveEnd += JoysticHanderMoveEnd; } void OnDisable() { //撤销事件 EasyJoystick.On_JoystickMoveStart -= JoystickMoveStart; EasyJoystick.On_JoystickMove -= JoystickHandlerMoving; EasyJoystick.On_JoystickMoveEnd -= JoysticHanderMoveEnd; } void JoystickMoveStart(MovingJoystick move) { Debug.Log("主角开始移动"); } void JoystickHandlerMoving(MovingJoystick move) { Debug.Log("主角移动中"); if (move.joystick.name != "Joystick") { return; } //获取虚拟摇杆偏移量 float Axis_x = move.joystickAxis.x; float Axis_y = move.joystickAxis.y; Vector3 position = this.transform.position; //设置角色的面向位置 this.transform.LookAt(new Vector3(position.x - Axis_x, position.y, position.z - Axis_y)); //主角移动 this.transform.Translate(Vector3.forward * Time.deltaTime * speed); } void JoysticHanderMoveEnd(MovingJoystick move) { Debug.Log("主角移动结束"); } }然后呢,是摇杆控制2D移动: 代码差别不大,也是挂在物体身上。
using UnityEngine; using System.Collections; public class PlayByJoyStick : MonoBehaviour { public EasyJoystick joystick;//虚拟摇杆 private float Axis_x, Axis_y; private RectTransform rectImage; void Start() { rectImage = GetComponent<RectTransform>(); } void OnEnable() { //注册事件 EasyJoystick.On_JoystickMoveStart += JoystickMoveStart; EasyJoystick.On_JoystickMove += JoystickHandlerMoving; EasyJoystick.On_JoystickMoveEnd += JoysticHanderMoveEnd; } void Update() { //Debug.Log("X" + Axis_x); //Debug.Log("Y" + Axis_y); } void OnDisable() { //撤销事件 EasyJoystick.On_JoystickMoveStart -= JoystickMoveStart; EasyJoystick.On_JoystickMove -= JoystickHandlerMoving; EasyJoystick.On_JoystickMoveEnd -= JoysticHanderMoveEnd; } void JoystickMoveStart(MovingJoystick move) { Debug.Log("主角开始移动"); } void JoystickHandlerMoving(MovingJoystick move) { Debug.Log("主角移动中"); if (move.joystick.name != "New joystick") { return; } //获取虚拟摇杆偏移量 Axis_x = move.joystickAxis.x; Axis_y = move.joystickAxis.y; //主角移动 rectImage.anchoredPosition +=new Vector2 (Axis_x, Axis_y); } void JoysticHanderMoveEnd(MovingJoystick move) { Debug.Log("主角移动结束"); } }Ok,暂时就这样吧。