当前位置:网站首页>猴子都会用的圆形滑动自动吸附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 圆周运动
边栏推荐
- Why is the service implementation class always red
- 嵌入式DSP音频开发
- 一位博士在华为的22年(干货满满)
- Windows 2003 64 bit system PHP running error: 1% is not a valid Win32 Application
- Dataloader parameter collate_ Use of FN
- INFO:&nbsp;HHH000397:&nbsp;Using…
- Thesis reading: General advantageous transformers
- Yes, it's about water
- 文件的相对路径写法
- 买股票通过券商经理的开户链接开户资金是否安全?想开户炒股
猜你喜欢

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

无代码软件发展简史及未来趋势

调试利器 go-spew

论文阅读:Generative Adversarial Transformers

剑指 Offer 47. 礼物的最大价值(DP)

Artifact for converting pcap to JSON file: joy (installation)

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

建立自己的网站(17)

Object类,以及__new__,__init__,__setattr__,__dict__

Set drop-down options on Excel files
随机推荐
嵌入式软件开发中必备软件工具
在excel文件上设置下拉选项
同样是MB,差距怎么这么大呢?
访问网站提示:您未被授权查看该页恢复办法
【小游戏】跑酷
apache、iis6、ii7独立ip主机屏蔽限制ip访问
Win10 如何删除系统盘大文件hiberfil.sys
为什么OpenCV计算的帧率是错误的?
在牛客中使用JS编程题【split】
collections. Use of defaultdict()
导致系统性能失败的十个原因
数据库的迁移
【小程序】使用font-awesome字体图标的解决文案(图文)
RichView TRVStyle
crond BAD FILE MODE /etc/cron.d
RichView TRVStyle
基于流的深度生成模型
PPT制作小技巧
文件的相对路径写法
2022 electrician (elementary) recurrent training question bank and online simulation examination