美文网首页
Unity的超简易摇杆

Unity的超简易摇杆

作者: 神大人korose | 来源:发表于2018-03-20 13:59 被阅读0次

需要用到上一篇的button监听实现

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Joystick : MonoBehaviour {
    public Rigidbody2D _playerRigid;
    public RectTransform dot;
    private RectTransform node;
    public float _speed = 0;
    public float speed1 = 100;
    public float speed2 = 200;

    private float _radian;
    private float _angle;
    private Vector3 _stickPos;

    // Use this for initialization
    void Start () {
        node = this.gameObject.GetComponent<RectTransform>();
        Debug.Log(dot.gameObject.name);

        EventTriggerListener.Get(dot.gameObject).onDown = onBtnBegan;
        EventTriggerListener.Get(dot.gameObject).onMove = onBtnMoved;
        EventTriggerListener.Get(dot.gameObject).onUp = onBtnEnded;
    }
    
    // Update is called once per frame
    void Update () {
        _allDirectionsMove();
    }
    private void onBtnBegan(PointerEventData eventData)
    {
        if (Data.isTouch)
        {
            Debug.Log("Began");
            _touchStartEvent(eventData.position);
        }
    }
    private void onBtnMoved(PointerEventData eventData)
    {
        if (Data.isTouch)
        {
            Debug.Log("Moved");
            _touchMoveEvent(eventData.position);
        }
    }
    private void onBtnEnded(PointerEventData eventData)
    {
        if (Data.isTouch)
        {
            Debug.Log("Ended");
            _touchEndEvent();
        }
    }
    //全方向移动
    void _allDirectionsMove()
    {
        float x = Mathf.Cos(_angle * (Mathf.PI / 180)) * _speed;
        float y = Mathf.Sin(_angle * (Mathf.PI / 180)) * _speed;
        _playerRigid.velocity = new Vector2(x, y);
    }
    
     //计算两点间的距离并返回
    float _getDistance(Vector3 pos1, Vector3 pos2)
    {
        return Mathf.Sqrt(Mathf.Pow(pos1.x - pos2.x, 2) +
        Mathf.Pow(pos1.y - pos2.y, 2));
    }

    /*角度/弧度转换
    角度 = 弧度 * 180 / Math.PI
    弧度 = 角度 * Math.PI / 180*/
    //计算弧度并返回
    float _getRadian(Vector3 point)
    {
        _radian = Mathf.PI / 180 * this._getAngle(point);
        return _radian;
    }

    //计算角度并返回
    float _getAngle(Vector3 point)
    {
        _angle = Mathf.Atan2(point.y - node.localPosition.y, point.x - node.localPosition.x) * (180 / Mathf.PI);
        return _angle;
    }

     //设置实际速度
    void _setSpeed(Vector3 point)
    {
        //触摸点和遥控杆中心的距离
        var distance = this._getDistance(point, Vector3.zero);
        //圆圈半径
        var radius = node.sizeDelta.x / 2;
        //如果半径
        if (distance < radius)
        {
            this._speed = this.speed1;
            Debug.Log(this._speed);
        }
        else
        {
            this._speed = this.speed2;
            Debug.Log(this._speed);
        }
    }
    
    bool _touchStartEvent(Vector3 touchPos) {
        //触摸点与圆圈中心的距离
        var distance = this._getDistance(transform.InverseTransformPoint(touchPos), Vector3.zero);
        //圆圈半径
        var radius = node.sizeDelta.x / 2;
        // 记录摇杆位置,给touch move使用
        _stickPos = touchPos;
        var posX = node.localPosition.x + touchPos.x;
        var posY = node.localPosition.y + touchPos.y;
         //手指在圆圈内触摸,控杆跟随触摸点
        if(radius > distance)
        {
            dot.localPosition = transform.InverseTransformPoint(new Vector3(posX, posY, 0));
            return true;
        }
        return false;
    }

    void _touchMoveEvent(Vector3 touchPos)
    {
        var distance = this._getDistance(transform.InverseTransformPoint(touchPos), Vector3.zero);
        var radius = node.sizeDelta.x / 2;
        // 由于摇杆的postion是以父节点为锚点,所以定位要加上ring和dot当前的位置(stickX,stickY)
        var posX = node.localPosition.x + touchPos.x;
        var posY = node.localPosition.y + touchPos.y;
        if (radius > distance)
        {
            dot.localPosition = transform.InverseTransformPoint(new Vector3(posX, posY, 0));
        }
        else
        {
            //控杆永远保持在圈内,并在圈内跟随触摸更新角度
            var x = node.localPosition.x + Mathf.Cos(_getRadian(transform.InverseTransformPoint(new Vector3(posX, posY, 0)))) * radius;
            var y = node.localPosition.y + Mathf.Sin(_getRadian(transform.InverseTransformPoint(new Vector3(posX, posY, 0)))) * radius;
            dot.localPosition = new Vector3(x, y, 0);
        }
        //更新角度
        _getAngle(transform.InverseTransformPoint(new Vector3(posX, posY, 0)));
        //设置实际速度
        _setSpeed(transform.InverseTransformPoint(new Vector3(posX, posY, 0)));

    }

    void _touchEndEvent()
    {
        dot.localPosition = new Vector3(node.localPosition.x, node.localPosition.y, node.localPosition.z);
        _speed = 0;
    }
}

相关文章

网友评论

      本文标题:Unity的超简易摇杆

      本文链接:https://www.haomeiwen.com/subject/pcxbqftx.html