当前位置:网站首页>猴子都会用的圆形滑动自动吸附UI工具
猴子都会用的圆形滑动自动吸附UI工具
2022-06-28 02:36:00 【饿掉鱼】
两个脚本
只需要挂载其中一个,另外一个自动挂载
脚本A
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Circle : MonoBehaviour
{
/*
* 本脚本共分两部分
* 第一部分Circle,推荐挂载在所有要移动物体的父物体上
* 然后,将所有移动物体加入到go数组中
* 如果要实现吸附,需要为所有子物体添加Trigger和rigidbody并在吸附物体中添加目标物(建议碰撞体弄小一点,更加准确)
*
* 第二部分adsorbent,负责子物体的吸附
* 将有该脚本自动负责挂载
* 如果出现报错,如空引用。不需要吸附功能可以选择注释掉
*/
[Header("旋转速度")] public float speed = 0.2f;
[Header("要移动的物体"), SerializeField]
public GameObject[] go;
//注:如果长轴短轴相等,则圆形移动,否则椭圆
[Header("长轴长"), SerializeField]
private float Ellipse_a;
[Header("短轴长"), SerializeField]
private float Ellipse_b;
[Header("间隙"), SerializeField,Range(0,1f)]
public float AwakeAngle = 60f;
[Header("原点"), SerializeField]
private GameObject Point;
[Header("吸附参考物"), SerializeField]
private GameObject AdsGameObject;
[Header("吸附参考物角度"), SerializeField, Range(0f, 360f)]
public float AdsAngle;
[Range(0.97f, 0.999f)]
[Header("惯性降低程度,越小降低越快")] public float _inertiadown = 0.98f;
[Range(0f, 1f)]
[Header("惯性强度")] public float _inertiastrong = 0.2f;
[Range(0f, 0.01f)]
[Header("吸附强度。越高则停止旋转时吸附的越快")] public float adstime = 0.01f;
/// <summary>
/// 在其他脚本调用true将重置所有移动物体位置
/// </summary>
public static bool ResetMove = true;
public void OnEnable()
{
adsorbent.adsa = AdsAngle;
int i = 0;
foreach (GameObject child in go)
{
child.AddComponent<adsorbent>();
child.GetComponent<adsorbent>().father = gameObject;
child.GetComponent<adsorbent>().AdsObj = AdsGameObject;
child.GetComponent<adsorbent>().id = i++;
}
AdsGameObject.transform.position = new Vector3(Ellipse_X(Ellipse_a, AdsAngle) + Point.transform.position.x, Ellipse_Y(Ellipse_b, AdsAngle) + Point.transform.position.y, 0);
}
private void Update()
{
MouseLister();
_memoryangle *= _inertiadown;
angle += _memoryangle * Time.deltaTime * _inertiastrong;
}
float _memoryangle;//记忆角度差值制造惯性
public static float angle;
#region 椭圆路线行动
IEnumerator Move()
{
#if EditorMode
if (!Input.GetMouseButton(0))
{
yield return new WaitForSeconds(0.05f);
for (int i = 0; i < go.Length; i++)
go[i].transform.position = new Vector3(Ellipse_X(Ellipse_a, angle + 60 * i) + Point.transform.position.x, Ellipse_Y(Ellipse_b, angle + 60 * i) + Point.transform.position.y, 0);
ResetMove = false;
}
#else
yield return new WaitForSeconds(0.05f);
for (int i = 0; i < go.Length; i++)
go[i].transform.position = new Vector3(Ellipse_X(Ellipse_a, angle + AwakeAngle * i) + Point.transform.position.x, Ellipse_Y(Ellipse_b, angle + AwakeAngle * i) + Point.transform.position.y, 0);
ResetMove = false;
#endif
}
private float Ellipse_X(float a, float angle)
{
return a * Mathf.Cos(angle * Mathf.Rad2Deg);
}
private float Ellipse_Y(float b, float angle)
{
return b * Mathf.Sin(angle * Mathf.Rad2Deg);
}
#endregion
#region 鼠标旋转
public void MouseLister()
{
if (Input.GetMouseButton(1) || ResetMove || Mathf.Abs(_memoryangle) > 0.002f)
StartCoroutine(Move());
if ((Input.GetMouseButton(0) || ResetMove || Mathf.Abs(_memoryangle) >= adstime))
{
adsorbent.ads = false;
StartCoroutine(Move());
if (Input.GetMouseButton(0))
{
_memoryangle += Input.GetAxis("Mouse X") * Time.deltaTime * speed;
angle += Input.GetAxis("Mouse X") * Time.deltaTime * speed;
}
}
else adsorbent.ads = true;
}
#endregion
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
*
* Circle 脚本 附属脚本
* 不要手动挂载
*
*
*
*
*/
public class adsorbent : MonoBehaviour
{
public void OnTriggerStay(Collider other)
{
if (ads && other.name == AdsObj.name)
Circle.angle =-(father.GetComponent<Circle>().AwakeAngle*id -adsa);
}
public int id;
public static float adsa;
[HideInInspector]
public GameObject father;
public static bool ads;
[HideInInspector]
public GameObject AdsObj;
}
脚本参考:(73条消息) unity让物体做圆周运动、椭圆运动、双曲线运动_小小小小羽丶的博客-CSDN博客_unity 圆周运动
边栏推荐
猜你喜欢

matlab习题 —— 矩阵的常规运算

2022 electrician (elementary) recurrent training question bank and online simulation examination

调试利器 go-spew

2022年R1快開門式壓力容器操作特種作業證考試題庫及答案

Basic operation of stack (implemented in C language)

Question bank and answers of special operation certificate for R1 quick opening pressure vessel operation in 2022

一位博士在华为的22年(干货满满)

基于流的深度生成模型

View the SQL execution plan according to explain and optimize the SQL

2022电工(初级)复训题库及在线模拟考试
随机推荐
Solution to not displaying logcat logs during debugging of glory V8 real machine
2022安全员-C证考试题库模拟考试平台操作
如何给Eclips自动添加作者,时间等…
【PaddleDetection】ModuleNotFoundError: No module named ‘paddle‘
windows 2003 64位系统php运行报错:1% 不是有效的 win32 应用程序
SSH框架的搭建(上)
Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队
没错,是水的一篇
RichView TRVStyle TextStyles
文件的相对路径写法
【PaddleDetection】ModuleNotFoundError: No module named ‘paddle‘
根据Explain查看sql执行计划,对SQL进行优化
Ten reasons for system performance failure
论文阅读:Generative Adversarial Transformers
CURDATE()和NOW()区别
一位博士在华为的22年(干货满满)
爱普生L3153打印机如何清洗喷头
学习---有用的资源
RichView TRVStyle TextStyles
【小游戏】跑酷