[英]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);
}
}
}
Some problems:一些问题:
The circle is standing vertical and not like it should be horizontal.圆圈是垂直的,而不是应该是水平的。
How can I make that I will be able to change in run time the radius size and the circle width?我怎样才能使我能够在运行时改变半径大小和圆的宽度?
How can I make that in the editor mode before running the game it will not show the line renderer first point near the target object that it should be around?如何在运行游戏之前在编辑器模式下使其不会显示线渲染器第一个点附近的目标 object 应该在附近?
The circle in run time is not completed at the top of it there is a place with some space or a missing part.运行时的圆圈没有在它的顶部完成,有一个地方有一些空间或缺失的部分。
This screen show is showing the start point or the line renderer in pink near the object before running the game:此屏幕显示在运行游戏之前在 object 附近以粉红色显示起点或线渲染器:
This screenshot is showing the drawn circle of the radius and the linerenderer the script settings in run time:此屏幕截图显示了半径的绘制圆和运行时脚本设置的 linerenderer:
To make the circle horizontal, vary the Z coordinate instead of the Y coordinate:要使圆水平,请改变 Z 坐标而不是 Y 坐标:
// ...
x = Mathf.Sin(Mathf.Deg2Rad * angle) * xradius;
y = Mathf.Cos(Mathf.Deg2Rad * angle) * yradius;
line.SetPosition(i, new Vector3(x, 0f, y));
// ..
To change the line width, vary the value of line.widthMultiplier
:要更改线宽,请改变
line.widthMultiplier
的值:
line.widthMultiplier = 2f;
To change the radius, alter xradius
and yradius
then call CreatePoints
again:要更改半径,请更改
xradius
和yradius
,然后再次调用CreatePoints
:
xradius = yradius = 10f;
CreatePoints();
The circle appears "incomplete" because the ends of the line renderer don't overlap sufficiently.圆圈看起来“不完整”,因为线渲染器的末端没有充分重叠。 To fix this, go further than 360 degrees by changing the
360f
in the last line to something greater.为了解决这个问题,通过将最后一行中的
360f
更改为更大的值,go 进一步超过 360 度。 For instance:例如:
// ...
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.