简体   繁体   English

如何使用 LineRenderer 根据 object 周围的半径大小绘制圆?

[英]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:要更改半径,请更改xradiusyradius ,然后再次调用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.

相关问题 如何使用 linerenderer 绘制一个圆圈并将其作为 object 的子 object 的圆圈应该画出来? - How can I draw a circle using linerenderer and make it the circle as object child of the object is should draw around? 我如何使用衬纸机.startColor和衬纸机.startWidth? - How can I use linerenderer.startColor and linerenderer.startWidth? 如何修复使用LineRenderer制作的不完美圆? - How can I fix this imperfect circle I made using LineRenderer? 我如何在Bing Map应用程序上以一个以英里为单位给出的半径为中心画一个圆 - How do I draw a circle on my Bing Map application around a centerpoint with a radius given in miles 如何围绕特定的 object 和/或围绕鼠标单击 position 画一个圆圈? - How to draw a circle around specific object and/or around the mouse click position? 如何将某个对象绕另一个对象绕圈移动? - How can I move some object in circle around another object? 如何将 object 移动到 linerenderer 线位置? - How can I move an object over linerenderer lines positions? 如何在 linerenderer 的两条连接线的每个角上绘制/显示文本? - How can I draw/display a text on each corner of two connected line of linerenderer? 如何在unity3d中围绕对象绘制圆圈? - How to draw circle around an object in unity3d? 如何通过背景减法在对象周围绘制矩形 - How can I draw a rectangle around an object from background subtraction
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM