当前位置:网站首页>猴子都会用的圆形滑动自动吸附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 圆周运动
边栏推荐
猜你喜欢

被校园暴力,性格内向的马斯克凄惨而励志的童年

mysql获取当前时间是一年的第多少天

Build your own website (17)

Tips for visiting the website: you are not authorized to view the recovery method of this page

CI & CD 不可不知!

第二轮红队免费公开课来袭~明晚八点!

Brief history and future trend of codeless software

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

Object class, and__ new__,__ init__,__ setattr__,__ dict__

一位博士在华为的22年(干货满满)
随机推荐
WARN:&nbsp;SQL&nbsp;Error:&nbsp;…
基于 LNMP 搭建个人网站的填坑之旅
Why is the service implementation class always red
将PCAP转换为Json文件的神器:joy(安装篇)
Object class, and__ new__,__ init__,__ setattr__,__ dict__
Import an excel file, solve the problem of skipping blank cells without reading and moving the subscript forward, and return_ BLANK_ AS_ Null red
Yes, it's about water
Thesis reading: General advantageous transformers
SSH框架的搭建(上)
INFO:&nbsp;HHH000397:&nbsp;Using…
service实现类里面为何一直报红
導入Excel文件,解决跳過空白單元格不讀取,並且下標前移的問題,以及RETURN_BLANK_AS_NULL報紅
R language penalty logistic regression, linear discriminant analysis LDA, generalized additive model GAM, multiple adaptive regression splines Mars, KNN, quadratic discriminant analysis QDA, decision
剑指 Offer 49. 丑数(三指针法)
mysql获取当前时间是一年的第多少天
A16z:元宇宙解锁游戏基础设施中的新机遇
The same is MB. Why is the gap so large?
2022电工(初级)复训题库及在线模拟考试
No&nbsp;result&nbsp;defined&amp;nbsp…
【522. 最长特殊序列 II】