手机游戏的摇杆

来源:互联网 发布:预算软件 编辑:程序博客网 时间:2024/05/01 06:37

两个脚本:

第一个:挂在调整好位置的摇杆图片上:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/// <summary>
/// 挂在摇杆图片上
/// </summary>
public class JoyStickL : MonoBehaviour
{
   Transform rPoint;          //中心点的变换组件
    public Vector3 offsetL;              //偏移量
    private Vector2 originalPos;        //初始位置
    private Touch[] touches;            //屏幕上触控点数组
    public bool isMoveL;                 //是否开始触控
    private int touchIndex = -1;         //触控数组下标
    private Vector2 tempDir;            //临时偏移量
    void Awake()
    {
rPoint = transform;
    }
    void Start()
    {
        originalPos = transform.position;//屏幕坐标位置
    }
    void Update()
    {
        //得到触点数组
        touches = Input.touches;
        //只有一个触点时
        if (touches.Length == 1)
        {
            touchIndex = 0;
        }
        //如果出现大于1个触点
        else if (touches.Length > 1)
        {
            //多个触点中距离近的为当前的控制触点
            if (Vector3.SqrMagnitude(touches[0].position - originalPos) < Vector3.SqrMagnitude(touches[1].position - originalPos))//长度的平方(速度快),只读
            {
                touchIndex = 0;
            }
            else
            {
                touchIndex = 1;
            }
        }
        //有触点开启
        if (touches.Length > 0)
        {
            //如果触点不是取消或者抬起
            if (Input.GetTouch(touchIndex).phase != TouchPhase.Canceled && Input.GetTouch(touchIndex).phase != TouchPhase.Ended)
            {
                //距离遥感范围内
                if (Vector3.SqrMagnitude(touches[touchIndex].position - originalPos) <= 100*100*3.14f)//像素数
                {
                    //遥感开始控制,计算偏移量
                    OutOffset(touchIndex);
                    //开启遥控
                    isMoveL = true;
                }
                //超过距离但是遥感开启状态
                else if (isMoveL)
                {
                    ReturnOffset(touchIndex);
                }
            }
            //如果抬起的手指为控制遥感的手指,则遥感归位
            else
            {
                rPoint.position= originalPos;
                offsetL = Vector3.zero;
                isMoveL = false;
                touchIndex = -1;
            }
        }
    }
    /// <summary>
    /// 摇杆控制方法
    /// </summary>
    /// <param name="i"></param>
    void OutOffset(int index)
    {
        //红点位置追踪
        rPoint.position= touches[index].position;
        //距离过小则视为不偏移
        if (Vector3.SqrMagnitude(touches[index].position - originalPos) <=5)
        {
            offsetL = Vector3.zero;
        }
        else
        {
            //给偏移量赋值
            offsetL = new Vector3(touches[index].position.x - originalPos.x, 0, touches[index].position.y - originalPos.y);
            offsetL = offsetL.normalized;//归一化(只读),转为长度为1的向量
        }
    }
    /// <summary>
    /// 超过范围时,遥感控制方法
    /// </summary>
    /// <param name="i"></param>
    void ReturnOffset(int index)
    {
        //更新偏移向量
        tempDir = touches[index].position - originalPos;
        //更新红点的位置
        rPoint.position = originalPos + (tempDir.normalized) * 20;
        //给偏移量赋值
        offsetL = new Vector3(touches[index].position.x - originalPos.x, 0, touches[index].position.y - originalPos.y);
        offsetL = offsetL.normalized;
    }
}


第二个:挂在需要摇杆控制的角色身上

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


public class CharactMoveController : MonoBehaviour
{
    GameObject StickL;//摇杆
    public float rotateSpeed = 1f;//旋转速度
    public float moveSpeed = 100f;//移动速度
   
    private void Start()
    {
        StickL = GameObject.Find("Cav_UI").transform.FindChild("Im_Joy_Left").gameObject;
    }
    void FixedUpdate()
    {
        LeftJoyStick();
    }
    /// <summary>
    /// 摇杆
    /// </summary>
    void LeftJoyStick()
    {
        bool TouchL = StickL.GetComponent<JoyStickL>().isMoveL;//获取左摇杆图片上的JoyStick组件的isMoveL
        if (TouchL)//如果isMoveL=true
        {
            Vector3 offsetL = StickL.GetComponent<JoyStickL>().offsetL;//定义V3型变量offsetL,赋值为左摇杆的offsetL
            if (offsetL != Vector3.zero)//如果offsetL不等于0
            {
                //transform.position += new Vector3(transform.forward.x, 0, transform.forward.z).normalized * offset.z* Time.deltaTime * moveSpeed;
                transform.Translate(Vector3.forward.normalized * offsetL.z * Time.deltaTime * moveSpeed);//向z轴正方向归一化乘以offsetL的z乘以移动速度乘以时间
                //transform.eulerAngles += new Vector3(0, offset.x, 0) * Time.deltaTime * rotateHorSpeed;
                transform.Rotate(Vector3.up.normalized * offsetL.x * Time.deltaTime * rotateSpeed);//绕y轴正方向归一化乘以offsetL的x乘以旋转速度乘以时间
            }
        }
    }
  }

原创粉丝点击