Unity 手机端 操纵杆的实现

绑定在一个空对象上

指定两个材质图片

一个是操纵杆主体,一个是代表操纵杆方向的小圆点

游戏运行时,就会显示操纵杆

using UnityEngine;
public class PhoneControl : MonoBehaviour
{
    public Texture Point, Ring;
    public static float x, y;
    Vector2 Posi, Pos0, Pos00, Pos_;
    float RingW, RingH, PointW, PointH;
    bool Drag;

    void Start()
    {
        Pos00 = Pos0 = Posi = new Vector2(300, 800);
        RingW = RingH = 400;
        PointW = PointH = 150;
        Drag = false;
        x = y = 0;
    }

    void Update()
    {
       
    }

    void OnGUI()
    {
        if (Event.current.type == EventType.MouseDown)
        {

            //是否需要移动控件
            Vector2 Posq = Event.current.mousePosition;
            float vz0;
            vz0 = (Posq.x - Pos0.x) * (Posq.x - Pos0.x) + (Posq.y - Pos0.y) * (Posq.y - Pos0.y);
            vz0 = Mathf.Sqrt(vz0);
            if (vz0 < RingW / 2)
            {
                Pos0 = Event.current.mousePosition;
                Drag = true;
            }
        }

        //抬起手指,归位
        if (Event.current.type == EventType.MouseUp)
        {
            Drag = false;
            Pos0 = Pos00;
        }

        //拖拽
        x = 0;
        y = 0;
        float vx = 0, vy = 0, vz;

        Pos_ = Event.current.mousePosition;
        vz = (Pos_.x - Pos0.x) * (Pos_.x - Pos0.x) + (Pos_.y - Pos0.y) * (Pos_.y - Pos0.y);
        vz = Mathf.Sqrt(vz);
        if (Drag) Posi = Pos_;
        else Posi = Pos0;

        if (vz > RingW / 2)
        {
            vx = (Posi.x - Pos0.x) / vz * RingW / 2 + Pos0.x;
            vy = (Posi.y - Pos0.y) / vz * RingH / 2 + Pos0.y;
            Posi = new Vector2(vx, vy);
        }

        GUI.DrawTexture(new Rect(Posi.x - PointW / 2, Posi.y - PointH / 2, PointW, PointH), Point);
        GUI.DrawTexture(new Rect(Pos0.x - RingW / 2, Pos0.y - RingH / 2, RingW, RingH), Ring);

        //外部速度接口
        if (Drag) Posi = Event.current.mousePosition;
        else Posi = Pos0;
        vz = (Posi.x - Pos0.x) * (Posi.x - Pos0.x) + (Posi.y - Pos0.y) * (Posi.y - Pos0.y);
        vz = Mathf.Sqrt(vz);
        x = Mathf.Abs(Posi.x - Pos0.x) / (RingW / 2) * (Posi.x - Pos0.x) / vz;
        y = -Mathf.Abs(Posi.y - Pos0.y) / (RingH / 2) * (Posi.y - Pos0.y) / vz;
        if (vz == 0) x = y = 0;
        if (vz > RingW / 2)
        {
            x = (Posi.x - Pos0.x) / vz;
            y = -(Posi.y - Pos0.y) / vz;
        }
        //其它脚本获得速度方向时,可写作:
        //gameObject.transform.Translate(Time.deltaTime * Speed * PhoneControl.x, Time.deltaTime * Speed * PhoneControl.y, 0);
    }
}

玩家脚本调用

        gameObject.transform.Translate(Time.deltaTime * speed * PhoneControl.x, Time.deltaTime * speed * PhoneControl.y, 0);

        float moveX=0;
        float moveY=0;



        //获取运动矢量
        moveX = PhoneControl.x;
        moveY = PhoneControl.y;

        Vector2 moveVector = new Vector2(moveX, moveY);

        if (moveX!=0||moveY!=0)
        {
            lookDirection = moveVector;
        }

        Vector2 position = transform.position;
        position.x += moveX * speed * Time.deltaTime;
        position.y += moveY * speed * Time.deltaTime;

        transform.position = new Vector2(position.x, position.y);
        //用刚体移动可以消除画面抖动
        rbody.MovePosition(position);

        //为动画集赋值
        //状态集的切换由运动矢量决定
        anim.SetFloat("move X",lookDirection.x);
        anim.SetFloat("move Y",lookDirection.y);
        anim.SetFloat("runspeed", moveVector.magnitude);
        //赋值必须与状态集命名一样

猜你喜欢

转载自blog.csdn.net/weixin_43673589/article/details/106891743