Unity3d虚拟摇杆的控制

作者: 潇千忘 | 来源:发表于2019-04-14 10:36 被阅读3次

    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;

    public class Rocker : MonoBehaviour {

        public UIButton rocker;

        public Transform center;//小圆

        public float ranger; //小圆移动的范围

        public Vector3 dir;//人物移动的向量

        //判断是否获取焦点

        public bool isPress = false;

        //单例模式

        public static Rocker ins;

        private void Awake()

        {

            ins = this;

        }

        // Use this for initialization

        void Start () {

            center = transform.Find("Center");

            //rocker.onClick.Add(new EventDelegate(RockerEvent));

        }

    // Update is called once per frame

    void Update () {

     

    }

        void OnPress( bool press) {

            isPress = press;

            RockerEvent();

        }

       

        public void RockerEvent() {

            StartCoroutine(CenterMove());

        }

        IEnumerator CenterMove() {

            while (Input.GetMouseButton(0))

            {

                Vector3 pos = UICamera.mainCamera.ScreenToWorldPoint(Input.mousePosition);

                //获取偏移值

                Vector3 offset = pos - transform.position;

                //magnitude求出向量长度

                if (offset.magnitude > ranger)

                {

                    //normalized归一化,求出单位向量

                    offset = offset.normalized*ranger;

                }

                //给小圆赋值

                center.position = transform.position + offset;

                dir = new Vector3(offset.x,0,offset.y);

                yield return null;

            }

            center.position = transform.position;

            dir = Vector3.zero;

        }

    }

    相关文章

      网友评论

        本文标题:Unity3d虚拟摇杆的控制

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