Unity3d虚拟摇杆的控制

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;

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 年少的时候,我们是不是各自有许多的秘密。 留给童年的秘密。留给夏天的秘密。留给操场的秘密。 留给时光的秘密。 留给...
    梁雪微阅读 603评论 0 0
  • 父母是我们和死亡之间隔着的一道帘子。 这是出自马尔克斯«百年孤独»中的一句话。乍看令人心惊。 忽然明白了,面对父母...
    锦瑟_db50阅读 377评论 2 2