![](/img/trans.png)
[英]How can I draw a circle using linerenderer and make it the circle as object child of the object is should draw around?
[英]How can I use LineRenderer to draw a circle depending on radius size around an object?
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(LineRenderer))]
public class DrawRadiusAround : MonoBehaviour
{
[Range(0, 50)]
public int segments = 50;
[Range(0, 5)]
public float xradius = 5;
[Range(0, 5)]
public float yradius = 5;
LineRenderer line;
void Start()
{
line = gameObject.GetComponent<LineRenderer>();
line.positionCount = segments + 1;
line.useWorldSpace = false;
CreatePoints();
}
void CreatePoints()
{
float x;
float y;
float z;
float angle = 20f;
for (int i = 0; i < (segments + 1); i++)
{
x = Mathf.Sin(Mathf.Deg2Rad * angle) * xradius;
y = Mathf.Cos(Mathf.Deg2Rad * angle) * yradius;
line.SetPosition(i, new Vector3(x, y, 0));
angle += (360f / segments);
}
}
}
一些问题:
圆圈是垂直的,而不是应该是水平的。
我怎样才能使我能够在运行时改变半径大小和圆的宽度?
如何在运行游戏之前在编辑器模式下使其不会显示线渲染器第一个点附近的目标 object 应该在附近?
运行时的圆圈没有在它的顶部完成,有一个地方有一些空间或缺失的部分。
此屏幕显示在运行游戏之前在 object 附近以粉红色显示起点或线渲染器:
此屏幕截图显示了半径的绘制圆和运行时脚本设置的 linerenderer:
要使圆水平,请改变 Z 坐标而不是 Y 坐标:
// ...
x = Mathf.Sin(Mathf.Deg2Rad * angle) * xradius;
y = Mathf.Cos(Mathf.Deg2Rad * angle) * yradius;
line.SetPosition(i, new Vector3(x, 0f, y));
// ..
要更改线宽,请改变line.widthMultiplier
的值:
line.widthMultiplier = 2f;
要更改半径,请更改xradius
和yradius
,然后再次调用CreatePoints
:
xradius = yradius = 10f;
CreatePoints();
圆圈看起来“不完整”,因为线渲染器的末端没有充分重叠。 为了解决这个问题,通过将最后一行中的360f
更改为更大的值,go 进一步超过 360 度。 例如:
// ...
x = Mathf.Sin(Mathf.Deg2Rad * angle) * xradius;
y = Mathf.Cos(Mathf.Deg2Rad * angle) * yradius;
line.SetPosition(i, new Vector3(x, 0f, y));
angle += (380f / segments);
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.