gpt4 book ai didi

c# - 我怎样才能使用 linerenderer 绘制一个圆圈,并使它成为圆圈,因为对象的子对象应该围绕它绘制?

转载 作者:行者123 更新时间:2023-12-05 07:03:48 27 4
gpt4 key购买 nike

当我将这个脚本附加到一个对象时,它会在它周围画一个圆圈。现在我想让脚本将 linerenderer 附加到空游戏对象,该游戏对象将成为将在其周围绘制圆圈的对象的子对象。

我想要那个,这样我就可以拖动空的游戏对象并更改圆的高度。这样我就可以简单地复制空的游戏对象并在地面上有两个圆圈。并且两个圆在父对象周围的半径相同,父对象将是中心。

using UnityEngine;
using System.Collections;
using System;

[ExecuteAlways]
[RequireComponent(typeof(LineRenderer))]
public class DrawRadiusAroundTurret : MonoBehaviour
{
[Range(1, 50)] public int segments = 50;
[Range(1, 5)] public float xRadius = 5;
[Range(1, 5)] public float yRadius = 5;
[Range(0.1f, 5f)] public float width = 0.1f;
public bool controlBothXradiusYradius = false;
public bool draw = true;

[SerializeField] private LineRenderer line;

private void Start()
{
if (!line) line = GetComponent<LineRenderer>();

CreatePoints();
}

public void CreatePoints()
{
line.enabled = true;
line.widthMultiplier = width;
line.useWorldSpace = false;
line.widthMultiplier = width;
line.positionCount = segments + 1;

float x;
float y;

var angle = 20f;
var points = new Vector3[segments + 1];
for (int i = 0; i < segments + 1; i++)
{
x = Mathf.Sin(Mathf.Deg2Rad * angle) * xRadius;
y = Mathf.Cos(Mathf.Deg2Rad * angle) * yRadius;

points[i] = new Vector3(x, 0f, y);

angle += (380f / segments);
}

// it's way more efficient to do this in one go!
line.SetPositions(points);
}

#if UNITY_EDITOR
private float prevXRadius, prevYRadius;
private int prevSegments;
private float prevWidth;

private void OnValidate()
{
// Can't set up our line if the user hasn't connected it yet.
if (!line) line = GetComponent<LineRenderer>();
if (!line) return;

if (!draw)
{
// instead simply disable the component
line.enabled = false;
}
else
{
// Otherwise re-enable the component
// This will simply re-use the previously created points
line.enabled = true;

if (xRadius != prevXRadius || yRadius != prevYRadius || segments != prevSegments || width != prevWidth)
{
CreatePoints();

// Cache our most recently used values.
prevXRadius = xRadius;
prevYRadius = yRadius;
prevSegments = segments;
prevWidth = width;
}

if (controlBothXradiusYradius)
{
yRadius = xRadius;
}
}
}
#endif
}

The linerenderer and the script are both attached to the Missles Turrent instead I want them to be on empty gameobject child of the Missles Turrent and that it will keep drawing the same circle around the Missles Turrent but when it will be from the child object I will be able to change the circle height.

主要目标是在 Missles Turrent 周围形成多个圆圈,但我可以通过简单的拖动来更改圆圈的高度。

最佳答案

解决方案是创建空游戏对象作为子对象,每个子位置设置为 0,0,0然后为每个 child 添加一个 linerenderer 组件和脚本。

脚本:

using UnityEngine;
using System.Collections;
using System;

[ExecuteAlways]
[RequireComponent(typeof(LineRenderer))]
public class DrawRadiusAroundTurret : MonoBehaviour
{
[Range(1, 50)] public int segments = 50;
[Range(1, 5)] public float xRadius = 5;
[Range(1, 5)] public float yRadius = 5;
[Range(0.1f, 5f)] public float width = 0.1f;
public bool controlBothXradiusYradius = false;
public bool draw = true;

[SerializeField] private LineRenderer line;

private void Start()
{
if (!line) line = GetComponent<LineRenderer>();

CreatePoints();
}

//private void Update()
//{
//
//}

public void CreatePoints()
{
line.enabled = true;
line.widthMultiplier = width;
line.useWorldSpace = false;
line.widthMultiplier = width;
line.positionCount = segments + 1;

float x;
float y;

var angle = 20f;
var points = new Vector3[segments + 1];
for (int i = 0; i < segments + 1; i++)
{
x = Mathf.Sin(Mathf.Deg2Rad * angle) * xRadius;
y = Mathf.Cos(Mathf.Deg2Rad * angle) * yRadius;

points[i] = new Vector3(x, 0f, y);

angle += (380f / segments);
}

// it's way more efficient to do this in one go!
line.SetPositions(points);
}

#if UNITY_EDITOR
private float prevXRadius, prevYRadius;
private int prevSegments;
private float prevWidth;

private void OnValidate()
{
// Can't set up our line if the user hasn't connected it yet.
if (!line) line = GetComponent<LineRenderer>();
if (!line) return;

if (!draw)
{
// instead simply disable the component
line.enabled = false;
}
else
{
// Otherwise re-enable the component
// This will simply re-use the previously created points
line.enabled = true;

if (xRadius != prevXRadius || yRadius != prevYRadius || segments != prevSegments || width != prevWidth)
{
CreatePoints();

// Cache our most recently used values.
prevXRadius = xRadius;
prevYRadius = yRadius;
prevSegments = segments;
prevWidth = width;
}

if (controlBothXradiusYradius)
{
yRadius = xRadius;
}
}
}
#endif
}

截图:

Circles

关于c# - 我怎样才能使用 linerenderer 绘制一个圆圈,并使它成为圆圈,因为对象的子对象应该围绕它绘制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63115088/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com