(转)制作虚拟摇杆

转自:http://blog.csdn.net/qinyuanpei/article/details/49510021
创建摇杆控制器

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class JoyStick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler {
    //--------------------------- property ---------------------------

    public float joyStickRadius = 50;   //摇杆最大半径
    public float joyStickResetSpeed = 5;    //遥控重置速度

    private RectTransform selfTransform;    //自己的transform
    private bool isTouched = false;     //是否触摸
    private Vector2 originPosition;     //遥杆当前位置
    private Vector2 touchedAxis;        //摇杆的移动方向

    //读取touchedAxis的值时,不会大于最大半径
    public Vector2 TouchedAxis {
        get {
            if(touchedAxis.magnitude < joyStickRadius) {
                return touchedAxis.normalized / joyStickRadius;
            }
            return touchedAxis.normalized;
        }
    }

    //定义委托, 开始, 过程, 结束事件
    public delegate void JoyStickTouchBegin(Vector2 vec);
    public delegate void JoyStickTouchMove(Vector2 vec);
    public delegate void JoyStickTouchEnd();

    //注册触摸开始, 过程, 结束事件
    public event JoyStickTouchBegin OnJoyStickTouchBegin;
    public event JoyStickTouchMove OnJoyStickTouchMove;
    public event JoyStickTouchEnd OnJoyStickTouchEnd;

    //--------------------------- Interface ---------------------------
    //UI.Button 捕获当点击事件点下时
    public void OnPointerDown(PointerEventData eventData) {
        isTouched = true;
        touchedAxis = GetJoyStickAxis (eventData);
        if (this.OnJoyStickTouchBegin != null) {
            this.OnJoyStickTouchBegin (touchedAxis);
        }
    }

    //捕获当点击事件抬起时
    public void OnPointerUp(PointerEventData eventData) {
        isTouched = false;
        selfTransform.anchoredPosition = originPosition;
        touchedAxis = Vector2.zero;
        if (this.OnJoyStickTouchEnd != null) {
            this.OnJoyStickTouchEnd ();
        }
    }

    //捕获当点击事件移动时
    public void OnDrag(PointerEventData eventData) {
        touchedAxis = GetJoyStickAxis (eventData);
        if(this.OnJoyStickTouchMove != null) {
            this.OnJoyStickTouchMove (touchedAxis);
        }
    }

    //--------------------------- method ---------------------------

    void Start () {
        //初始化摇杆的默认方向
        selfTransform = this.GetComponent ();
        originPosition = selfTransform.anchoredPosition;
    }

    void Update () {
        //当虚拟摇杆移动到最大半径时, 摇杆无法移动
        //为了确保被控制物体可以继续移动, 在这里手动触发OnJoyStickTouchMove事件
        if(isTouched && touchedAxis.magnitude >= joyStickRadius) {
            if (this.OnJoyStickTouchMove != null) {
                this.OnJoyStickTouchMove (touchedAxis);
            }
        }
    }

    //返回摇杆偏移量
    private Vector2 GetJoyStickAxis(PointerEventData eventData) {
        //获取手指位置的世界坐标
        Vector3 worldPosition;
        if(RectTransformUtility.ScreenPointToWorldPointInRectangle(
            selfTransform, eventData.position, eventData.pressEventCamera, out worldPosition)) {
            selfTransform.position = worldPosition;
        }
        //获取摇杆的偏移量
        Vector2 theTouchAxis = selfTransform.anchoredPosition - originPosition;
        if(theTouchAxis.magnitude >= joyStickRadius) {
            theTouchAxis = theTouchAxis.normalized * joyStickRadius;
            selfTransform.anchoredPosition = theTouchAxis;
        }
        return theTouchAxis;
    }
}

根据摇杆移动角色

using UnityEngine;
using System.Collections;

public class JoyStick3D : MonoBehaviour {
    //--------------------------- property ---------------------------
    public float moveSpeed;
    private JoyStick joyStick;


    //--------------------------- method ---------------------------
    void Start () {
        joyStick = GameObject.FindObjectOfType ();
        joyStick.OnJoyStickTouchBegin += OnJoyStickBegin;
        joyStick.OnJoyStickTouchMove += OnJoyStickMove;
        joyStick.OnJoyStickTouchEnd += OnjoyStickEnd;
    }
    
    // Update is called once per frame
    void Update () {
    
    }


    //--------------------------- Event ---------------------------
    void OnJoyStickBegin(Vector2 vec) {
        
    }

    void OnJoyStickMove(Vector2 vec) {
        //设置角色朝向
        Quaternion q = Quaternion.LookRotation (new Vector3 (vec.x, 0, vec.y));
        transform.rotation = q;

        //移动角色并且播放奔跑动画
        transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
    }

    void OnjoyStickEnd() {
        
    }
}

你可能感兴趣的:((转)制作虚拟摇杆)