当前位置:网站首页>猴子都会用的圆形滑动自动吸附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 圆周运动
边栏推荐
- [games] Parkour
- Object class, and__ new__,__ init__,__ setattr__,__ dict__
- GAMES104 作业2-ColorGrading
- Import an excel file, solve the problem of skipping blank cells without reading and moving the subscript forward, and return_ BLANK_ AS_ Null red
- 同样是MB,差距怎么这么大呢?
- Dataloader parameter collate_ Use of FN
- 2022 electrician (elementary) recurrent training question bank and online simulation examination
- How to write concise code? (upper)
- 启牛开的证券账户是安全的吗?如何开账户呢
- 栈的基本操作(C语言实现)
猜你喜欢
被校园暴力,性格内向的马斯克凄惨而励志的童年
Importer un fichier Excel, résoudre le problème de sauter les cellules vides et de ne pas lire, et avancer l'indice, et retourner Blank As NULL Red
Redis cluster setup [simple]
Solution to not displaying logcat logs during debugging of glory V8 real machine
2022电工(初级)复训题库及在线模拟考试
调试利器 go-spew
Object class, and__ new__,__ init__,__ setattr__,__ dict__
Set drop-down options on Excel files
R language penalty logistic regression, linear discriminant analysis LDA, generalized additive model GAM, multiple adaptive regression splines Mars, KNN, quadratic discriminant analysis QDA, decision
将PCAP转换为Json文件的神器:joy(安装篇)
随机推荐
Build your own website (17)
Dataloader参数collate_fn的使用
Importer un fichier Excel, résoudre le problème de sauter les cellules vides et de ne pas lire, et avancer l'indice, et retourner Blank As NULL Red
Tencent games released more than 40 products and projects, including 12 new games
xml&nbsp;文件的读写
apache、iis6、ii7独立ip主机屏蔽拦截蜘蛛抓取(适用vps云主机服务器)
Ten years' experience of Software Engineer
用于 C# 的 SQL 基本语法总结
Notepad++--常用的插件
Win10 如何删除系统盘大文件hiberfil.sys
Artifact for converting pcap to JSON file: joy (installation)
PPT制作小技巧
導入Excel文件,解决跳過空白單元格不讀取,並且下標前移的問題,以及RETURN_BLANK_AS_NULL報紅
如何编写简洁代码?(上)
Dataloader parameter collate_ Use of FN
数据库乱码问题
多快好省,低门槛AI部署工具FastDeploy测试版来了!
MySQL错误
RichView TRVStyle ParaStyles
2022年R1快開門式壓力容器操作特種作業證考試題庫及答案