Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Random.insideUnitCirclea around a certain position?

Discussion in 'Scripting' started by pivotraze, Nov 27, 2012.

  1. pivotraze

    pivotraze

    Joined:
    Feb 4, 2012
    Posts:
    593
    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
    Code (csharp):
    1.  
    2. if dist > 60:
    3.     newPosition as Vector2 = Random.insideUnitCircle * 60
    4.     transform.position.x = newPosition.x
    5.     transform.position.y = newPosition.y
    6.  
    I know this doesn't really take into account any center... but I don't think you can do that, as http://docs.unity3d.com/Documentation/ScriptReference/Random-insideUnitCircle.html does say you can...
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Just add the position to Random.insideUnitCircle.

    Code (csharp):
    1. someObjectPosition = Vector3(100, 50, 0)
    2. newPosition = someObjectPosition + Random.insideUnitCircle * 60
    --Eric
     
    lordofduct likes this.
  3. pivotraze

    pivotraze

    Joined:
    Feb 4, 2012
    Posts:
    593
    How come this doesn't work?
    Code (csharp):
    1.  
    2. powerPillar as GameObject // Set in the editor
    3. if dist > 60:
    4.     newPosition as Vector2 = Random.insideUnitCircle * 60 + powerPillar.transform.position
    5.     transform.position.x = newPosition.x
    6.     transform.position.y = newPosition.y
    7.  
    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.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Not sure without seeing more of the code. You can simplify what you have there though:

    Code (csharp):
    1. transform.position = Random.insideUnitCircle * 60 + powerPillar.transform.position
    --Eric
     
  5. pivotraze

    pivotraze

    Joined:
    Feb 4, 2012
    Posts:
    593
    [REDACATED]
    Entire Script lol
     
    Last edited: Nov 28, 2012
  6. pivotraze

    pivotraze

    Joined:
    Feb 4, 2012
    Posts:
    593
    Fixed.

    Add the position of the object as a Vector2.

    Example:

    Code (csharp):
    1.  
    2. newPosition as Vector2 = Random.insideUnitCircle * 100 + Vector2(playerObj.transform.position.x, playerObj.transform.position.z)
    3.  
     
    Last edited: Nov 28, 2012
  7. SolAZDev

    SolAZDev

    Joined:
    Nov 26, 2013
    Posts:
    8
    I just did this, by looking at the examples in here. Hope it helps @u@ (although this is a dead topic)

    Code (CSharp):
    1. Vector2 randPos = Random.insideUnitCircle * multiplier;
    2. ObjectInQuestion.transform.position += new Vector3(randPos.x, 0, randPos.y);
    3.  
    4. //Alternatively look at a thing
    5. ObjectInQusestion.transform.eulerAngles = new Vector3(0, Quaterion.LookAt( Vector3.zero /* or anything else you wanna look at*/).eulerAngles.y, 0);
     
    firebird721 likes this.
  8. Ziplock9000

    Ziplock9000

    Joined:
    Jan 26, 2016
    Posts:
    360
    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.
     
    jipsen likes this.
  9. franciscochong

    franciscochong

    Joined:
    Jul 9, 2015
    Posts:
    30
    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?