Random.insideUnitCirclea around a certain position?

I want to use Random.insideUnitCircle to spawn my enemy around a specific object. However, I don’t really… know how to do that.

I have it assign a new position based on what Random.InsideUnitCircle finds, but it doesn’t work as planned. I’d like for this scripting to be in the enemy script, and not attached to the other gameobject, if possible.

*note, even though I script in Boo, feel free to script it in whatever else you like. I know how to convert between the three languages.

This is the current script

if dist > 60:
	newPosition as Vector2 = Random.insideUnitCircle * 60
	transform.position.x = newPosition.x
	transform.position.y = newPosition.y

I know this doesn’t really take into account any center… but I don’t think you can do that, as Unity - Scripting API: Random.insideUnitCircle does say you can…

Just add the position to Random.insideUnitCircle.

someObjectPosition = Vector3(100, 50, 0)
newPosition = someObjectPosition + Random.insideUnitCircle * 60

–Eric

1 Like

How come this doesn’t work?

powerPillar as GameObject // Set in the editor
if dist > 60:
	newPosition as Vector2 = Random.insideUnitCircle * 60 + powerPillar.transform.position
	transform.position.x = newPosition.x
	transform.position.y = newPosition.y

The enemy just moves around the pillar, but he constantly does it multiple times. It is under Update, but it should still only do it once, since after that, he should be within 60.

Not sure without seeing more of the code. You can simplify what you have there though:

transform.position = Random.insideUnitCircle * 60 + powerPillar.transform.position

–Eric

[REDACATED]
Entire Script lol

Fixed.

Add the position of the object as a Vector2.

Example:

newPosition as Vector2 = Random.insideUnitCircle * 100 + Vector2(playerObj.transform.position.x, playerObj.transform.position.z)

I just did this, by looking at the examples in here. Hope it helps @u@ (although this is a dead topic)

Vector2 randPos = Random.insideUnitCircle * multiplier;
ObjectInQuestion.transform.position += new Vector3(randPos.x, 0, randPos.y);

//Alternatively look at a thing
ObjectInQusestion.transform.eulerAngles = new Vector3(0, Quaterion.LookAt( Vector3.zero /* or anything else you wanna look at*/).eulerAngles.y, 0);
1 Like

Remember insideUnitCircle is a Vector2 which assigned X and Y… But you might be wanting coordinates on X and Z if they are to move on a landscape.

2 Likes

Hi all how can I use Random.insideUnitCircle to get a random position outside of the origin range?

for Example if I wanted to spawn objects in a random position inside the UnitCircle but not in the origin point or 5 units from origin how would the math / look like?