unity绘制一条流动的弧线(贝塞尔线)
作者:BBoy_Leon 时间:2022-09-03 18:15:00
本文实例为大家分享了unity绘制一条流动弧线的具体代码,供大家参考,具体内容如下
最终效果
把下面脚本复制,直接拖上脚本,设置两个点(物体)的位置
GameObject1是开始点的位置,GameObject2是结束点的位置
public Transform[] controlPoints;
public LineRenderer lineRenderer;
public float centerPoint =0.1f;
private int layerOrder = 0;
//生成弧线中间的点数
private int _segmentNum = 20;
//偏移
float m_offset;
float m_speed = 0.5f;
void Start()
{
if (!lineRenderer)
{
lineRenderer = GetComponent<LineRenderer>();
}
lineRenderer.sortingLayerID = layerOrder;
//调用画贝斯尔线
GetBeizerList(controlPoints[0].position, (controlPoints[0].position + controlPoints[1].position) * 0.5f + new Vector3(0, centerPoint, 0), controlPoints[1].transform.position, _segmentNum);
}
private void Update()
{
m_offset = m_offset - m_speed * Time.deltaTime;
//控制offset使材质移动
GetComponent<LineRenderer>().material.mainTextureOffset = new Vector2(m_offset, 0);
}
/// <summary>
/// 根据T值,计算贝塞尔曲线上面相对应的点
/// </summary>
/// <param name="t"></param>T值
/// <param name="p0"></param>起始点
/// <param name="p1"></param>控制点
/// <param name="p2"></param>目标点
/// <returns></returns>根据T值计算出来的贝赛尔曲线点
private static Vector3 CalculateCubicBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2)
{
float u = 1 - t;
float tt = t * t;
float uu = u * u;
Vector3 p = uu * p0;
p += 2 * u * t * p1;
p += tt * p2;
return p;
}
/// <summary>
/// 获取存储贝塞尔曲线点的数组
/// </summary>
/// <param name="startPoint"></param>起始点
/// <param name="controlPoint"></param>控制点
/// <param name="endPoint"></param>目标点
/// <param name="segmentNum"></param>采样点的数量
/// <returns></returns>存储贝塞尔曲线点的数组
public Vector3[] GetBeizerList(Vector3 startPoint, Vector3 controlPoint, Vector3 endPoint, int segmentNum)
{
Vector3[] path = new Vector3[segmentNum];
for (int i = 1; i <= segmentNum; i++)
{
float t = i / (float)segmentNum;
Vector3 pixel = CalculateCubicBezierPoint(t, startPoint,
controlPoint, endPoint);
//设置lineRenderer的control Points
lineRenderer.positionCount = i;
lineRenderer.SetPosition(i - 1, pixel);
//存储Point
path[i - 1] = pixel;
Debug.Log(path[i - 1]);
}
return path;
}
来源:https://blog.csdn.net/BBoyLXiang/article/details/88815637
标签:unity,弧线
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java Object定义三个点实现代码
2023-11-21 06:05:07
Java编程实现A*算法完整代码
2022-06-18 12:28:57
![](https://img.aspxhome.com/file/2023/1/72591_0s.png)
Android垂直切换的圆角Banner与垂直指示器相关介绍与应用详解
2023-01-09 02:59:15
![](https://img.aspxhome.com/file/2023/5/137885_0s.gif)
Android自定义ViewGroup实现弹性滑动效果
2022-07-25 19:44:08
![](https://img.aspxhome.com/file/2023/8/122078_0s.jpg)
Spring Security中的Servlet过滤器体系代码分析
2023-03-23 19:05:34
![](https://img.aspxhome.com/file/2023/3/92553_0s.png)
深入浅析hbase的优点
2021-08-06 19:27:58
![](https://img.aspxhome.com/file/2023/0/80270_0s.png)
SpringBoot+Dubbo+Zookeeper实现简单分布式开发的应用详解
2022-03-02 00:47:40
![](https://img.aspxhome.com/file/2023/8/83158_0s.png)
java基础的详细了解第五天
2023-06-02 16:54:04
![](https://img.aspxhome.com/file/2023/9/65349_0s.png)
springmvc 防止表单重复提交的两种方法
2023-03-27 17:57:18
dotnet如何将文件删除到回收站
2023-04-23 13:52:09
![](https://img.aspxhome.com/file/2023/8/119488_0s.jpg)
android获取手机唯一标识的方法
2022-09-16 15:55:39
Android通过实现GridView的横向滚动实现仿京东秒杀效果
2023-06-08 22:53:46
![](https://img.aspxhome.com/file/2023/7/138057_0s.png)
Java语言中&&与& ||与|的区别是什么
2022-06-10 17:44:09
Android实现登陆页logo随键盘收放动态伸缩(完美解决键盘弹出遮挡控件的问题)
2022-05-25 04:13:26
![](https://img.aspxhome.com/file/2023/6/128996_0s.gif)
java操作excel表格详解
2021-08-20 14:35:46
![](https://img.aspxhome.com/file/2023/9/76269_0s.png)
Java中Scanner类与BufferReader类的不同点(非常详细)
2023-07-07 01:21:18
springboot整合mybatis的超详细过程(配置模式+注解模式)
2023-10-03 09:54:03
![](https://img.aspxhome.com/file/2023/5/81225_0s.png)
C#弹出对话框确定或者取消执行相应操作的实例代码
2021-11-19 13:26:29
java中Class.forName的作用浅谈
2023-11-11 12:30:26
C#实现实体类和XML相互转换
2023-06-16 04:37:32