簡體   English   中英

如何在半徑上設置隨機位置?

[英]How can I set random positions on radius?

在這個腳本中,我正在創建一個具有特定半徑大小的圓並獲取半徑大小:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(LineRenderer))]
public class DrawRadiusAroundTurret : MonoBehaviour
{
    [Range(0, 50)]
    public int segments = 50;
    [Range(0, 5)]
    public float xradius = 5;
    [Range(0, 5)]
    public float yradius = 5;
    [Range(0.1f, 5f)]
    public float width = 0.1f;
    LineRenderer line;

    void Start()
    {
        line = gameObject.GetComponent<LineRenderer>();

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

    private void Update()
    {
        CreatePoints();
    }

    public Vector3[] CreatePoints()
    {
        line.widthMultiplier = width;

        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, 0f, y));

            angle += (380f / segments);
        }

        var positions = new Vector3[line.positionCount];

        return positions;
    }
}

在這個腳本中,我有這個方法,我希望在半徑邊緣位置上隨機設置最后一個點:

private void GeneratePointsInTracks()
    {
        var startPoints = GameObject.FindGameObjectsWithTag("Start Point");
        var curvedLines = GameObject.FindGameObjectsWithTag("Curved Line");

        for (int i = 0; i < startPoints.Length; i++)
        {
            for (int x = 0; x < numberOfPointsInTrack; x++)
            {
                GameObject go = Instantiate(tracksPrefab, curvedLines[i].transform);
                go.name = "Point In Track";
                go.transform.position = turrent.position + new Vector3(Random.Range(-100f, 100f), Random.Range(-100f, 100f), Random.Range(-100f, 100f));

                if(x == numberOfPointsInTrack - 1)
                {
                    go.name = "Last Point In Track";

                    for(int y = 0; y < drawRadius.CreatePoints().Length; y++)
                    {
                        go.transform.position = new Vector3(Random.Range(0,1)[y].x,
                            drawRadius.CreatePoints()[y].y,
                            drawRadius.CreatePoints()[y].z);
                    }
                }
            }
        }
    }

我試過這個:

 go.transform.position = new Vector3(Random.Range(0,1)[y].x,
                                drawRadius.CreatePoints()[y].y,
                                drawRadius.CreatePoints()[y].z);

但 x 上的隨機數給出錯誤:

無法將帶有 [] 的索引應用於“int”類型的表達式

第一個腳本創建一個像這樣的圓圈:

圓圈

這是我在油漆中繪制的一個示例,只是為了說明我的意思是我說我希望第二個腳本中的端點在圓邊上隨機地是 position:

例子

因此,每個“跟蹤中的最后一個點”object 應該是 position 在圓邊上隨機出現,如第二個屏幕截圖所示。

我想你想要的是這樣的:

Vector3[] points = drawRadius.CreatePoints(); //get all edge points
Vector3 randomPoint = points[Random.Range(0, points.Length)]; //pick a random one

go.transform.position = randomPoint; //set go.transform.position to position of random point

對不起,如果我誤解了你的意圖,但我希望這對你有用!

如果您知道中心和半徑,則很容易在圓上獲得隨機點:

go.name = "Last Point In Track";
Vector2 p = Random.insideUnitCircle.normalized * radius;
go.transform.position = center + new Vector3(p.x, 0, p.y);

要擺脫 Random.insideUnitCircle 太小而無法標准化的邊緣情況,您應該使用:

Vector2 RandomOnUnitCircle(){
    Vector2 result = Vector2.zero;
    do{
        result = Random.insideUnitCircle.normalized;
    }while(result == Vector2.zero);
    return result;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM