Unity实现简单的虚拟摇杆
作者:RiKoPon 发布时间:2023-01-26 11:38:14
标签:Unity,虚拟摇杆
本文实例为大家分享了Unity实现简单虚拟摇杆的具体代码,供大家参考,具体内容如下
需求:点击创建一个虚拟摇杆底盘,鼠标拖拽时候上方摇杆会跟随鼠标方向移动,并且不会超出摇杆盘范围
*摇杆功能另外实现
UI显示
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RockingIcon : MonoBehaviour
{
public Transform touchPoint;
public Transform bgPoint;
public float radius;
bool isPressing;
Vector3 bgPos;
private void Update()
{
bool pressing;
Vector3 pos;
if (Application.isEditor)
GetPressingInfoInEditor(out pressing, out pos);
else
GetPressingInfoInPhone(out pressing, out pos);
SetIcon(pressing, pos);
}
void GetPressingInfoInEditor(out bool pressing, out Vector3 pos)
{
if (Input.GetMouseButton(0))
{
pressing = true;
pos = Input.mousePosition;
}
else
{
pressing = false;
pos = Vector3.zero;
}
}
void GetPressingInfoInPhone(out bool pressing, out Vector3 pos)
{
if(Input.touchCount > 0)
{
pressing = true;
pos = Input.GetTouch(0).position;
}
else
{
pressing = false;
pos = Vector3.zero;
}
}
void SetIcon(bool pressing, Vector3 pos)
{
if (pressing)
{
if (!isPressing)
{
bgPoint.gameObject.SetActive(true);
bgPoint.transform.position = pos;
bgPos = pos;
isPressing = true;
}
else
{
bgPoint.gameObject.SetActive(true);
SetTouchPointPos(pos);
}
}
else
{
touchPoint.gameObject.SetActive(false);
bgPoint.gameObject.SetActive(false);
isPressing = false;
}
}
void SetTouchPointPos(Vector3 pos)
{
Vector3 center = bgPoint.position;
Vector3 touch = pos;
Vector3 to;
float distance = Vector3.Distance(center, touch);
if (distance < radius)
to = touch;
else
{
Vector3 dir = touch - center;
dir.Normalize();
to = dir * radius;
to += center;
}
touchPoint.gameObject.SetActive(true);
touchPoint.transform.position = to;
}
}
预制:
操作控制
#region 鼠标操作
float min_move_x = Global.min_move_distance * (Screen.width / 1080f);
float min_move_y = Global.min_move_distance * (Screen.height / 1900f);
if(Application.platform == RuntimePlatform.WindowsEditor)
{
if (Input.GetMouseButtonDown(0))
{
touch_time = 0;
first_touch_pos = Input.mousePosition;
}
else if (Input.GetMouseButton(0))
{
touch_time += Time.deltaTime;
if (touch_time >= Global.touch_time_limit)
{
Vector2 touch_pos = Input.mousePosition;
Vector2 distance = touch_pos - first_touch_pos;
//Vector2 touch_pos_in_func = PosInTheFunc(touch_pos);
//Vector2 first_pos_in_func = PosInTheFunc(first_touch_pos);
//Vector2 distance = touch_pos_in_func - first_pos_in_func;
if (Mathf.Abs(distance.x) > min_move_x && Mathf.Abs(distance.x) > Mathf.Abs(distance.y)) Move(distance.x > 0 ? Vector3.right : Vector3.left);
if (Mathf.Abs(distance.y) > min_move_y && Mathf.Abs(distance.y) > Mathf.Abs(distance.x)) Move(distance.y > 0 ? Vector3.forward : Vector3.back);
}
}
else if (Input.GetMouseButtonUp(0))
{
//if(touch_time < Global.touch_time_limit)
//{
// PutBoomb();
//}
touch_time = 0;
first_touch_pos = Vector3.zero;
}
}
#endregion
#region 手机操作
if (Application.platform == RuntimePlatform.Android)
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
first_touch_pos = touch.position;
}
else if (touch.phase == TouchPhase.Ended)
{
first_touch_pos = Vector3.zero;
}
else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
{
Vector2 touch_pos = touch.position;
Vector2 distance = touch_pos - first_touch_pos;
if (Mathf.Abs(distance.x) > min_move_x && Mathf.Abs(distance.x) > Mathf.Abs(distance.y)) Move(distance.x > 0 ? Vector3.right : Vector3.left);
if (Mathf.Abs(distance.y) > min_move_y && Mathf.Abs(distance.y) > Mathf.Abs(distance.x)) Move(distance.y > 0 ? Vector3.forward : Vector3.back);
}
}
}
来源:https://blog.csdn.net/qq_33205561/article/details/89028711


猜你喜欢
- 前言此节假日为严格按照国家要求的双休和法定节假日并且包含节假日的补班信息,大家可根据自己的需求自定义处理哦。以下为Maven配置,是程序用到
- 调用后动态壁纸其实是显示在Activity的后面,而Activity则是透明显示,这样就可以看到下面的动态壁纸,如果Activity不是透明
- 在面向对象设计原则中,要求"要依赖于抽象,不要依赖于具体", 这句话有很多人搞不懂。在这里谈谈我自己的理解。首先看看以下
- 一、项目简述功能包括: 前台+后台健身房管理系统,用户预订,教练选择。课程选 择,登录,后台管理等等。二、项目运行环境配置: Jdk1.8
- using System;using System.Collections;using System.IO;namespace Consol
- 本篇给大家详细讲解了MTKAndroid平台开发流程,大致分为44个步骤,我们把每个步骤的命令详细讲解了下,一起来学习下。1.拷贝代码仓库从
- Spring Boot如何实现分布式锁的自动释放在分布式系统中,为了保证数据的一致性和可靠性,常常需要使用分布式锁。在实际开发中,我们可以使
- 本文实例为大家分享了Android使用Gridview单行横向滚动显示的具体代码,供大家参考,具体内容如下要想实现滚动显示,layout布局
- listview经常结合下来刷新和上拉加载更多使用,本文总结了三种常用到的方案分别作出说明。
- 本文实例讲述了Android实现EditText控件禁止输入内容的方法。分享给大家供大家参考,具体如下:问题:android如何实现Edit
- Java线程的概念和其他多数计算机语言不同,Java内置支持多线程编程(multithreaded programming)。多线程程序包含
- 前两年写的东西,现在整理一下发出来!以前公司需要做WebService,并且对WebService的SoapHeader进行加密,所以就写了
- Android Support Annotations &
- Model与Session区别什么是Session:Session:在计算机中,尤其是在网络应用中,称为“会话”。它具体是指一个终端用户与交
- 本文实例讲述了C#中使用ADOMD.NET查询多维数据集的实现方法,分享给大家供大家参考。具体实现方法分析如下:ADOMD.NET 是用于与
- 笔者发现在很多应用中,都有自动获取验证码的功能:点击获取验证码按钮,收到短信,当前应用不需要退出程序就可以获取到短信中的验证码,并自动填充。
- 左右滑动是智能手机最常用的动作,在此简单的封装了一下,以后直接拿来用就可以了。简单的只需要几行就可以了,下面那个类是封装好了的。packag
- 线程池做什么网络请求通常有两种形式:第一种,请求不是很频繁,而且每次连接后会保持相当一段时间来读数据或者写数据,最后断开,如文件下载,网络流
- 1. JVM 运行时数据区JVM运行时数据区可以分为元空间,堆,虚拟机栈,本地方法栈,程序计数器五大块。元空间(方法区):存放类模版对象,是
- 研究其父类时候发现,可以设置这么一条属性,在AlertDialog.Builder.create()之后才能调用这两个方法方法一:setCa