1

In this script I'm creating a circle with specific radius size and get the radius size :

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; } } 

And in this script I have this method and I want the last point/s to be set randomly on the radius edge 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); } } } } } 

I tried this :

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

but the random on the x give error :

Cannot apply indexing with [] to an expression of type 'int'

The first script create a circle like this :

Circle

And this is an example I drawed in paint just to show what I mean that I said I want the endPoints in the second script to be position randomly on the circle edges :

Example

So each "Last Point In Track" object should be position randomly on the circle edge like in the second screenshot.

6
  • 2
    Random.Range(0,1) is just a number, not a collection, don't try to access it with an index y Commented Jul 24, 2020 at 22:22
  • 2
    Also in the end of the first snippet you do var positions = new Vector3[line.positionCount]; return positions; so you always return an array with zero vectors, which is not what you want Commented Jul 24, 2020 at 22:25
  • 1
    Do you want to pick random positions from the ones you already set in the LineRenderer, or just random positions on the circle? Commented Jul 24, 2020 at 22:51
  • @Pluto I want that the endPoint in the second script each endPoint to be positioned on a random position on the circle from the first script. Commented Jul 24, 2020 at 23:03
  • @Pluto I updated my question with two screenshots at the bottom explain what I mean. Commented Jul 24, 2020 at 23:08

2 Answers 2

1

If you know the center and radius it is fairly easy to get random points on the circle:

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

To get rid of the edge case where Random.insideUnitCircle is to small to be normalized you should use:

Vector2 RandomOnUnitCircle(){ Vector2 result = Vector2.zero; do{ result = Random.insideUnitCircle.normalized; }while(result == Vector2.zero); return result; } 
Sign up to request clarification or add additional context in comments.

3 Comments

How can I return or get from the first script CreatePoints the circle radius and center ?
In the second script at the top I added public DrawRadiusAroundTurret drawRadius; so I will be able to access the CreatePoints method from the first script but I'm not sure how to get both the radius and center.
Radius should be drawRadius.xradius and center drawRadius.transform.position if you have the circle around the gameobject that has the DrawRadiusAroundTurret script attached.
1

I think what you want is this:

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 

Sorry if I am misunderstanding your intentions, but I hope this works for you!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.