- Home /
Spawn objects at random position ON a 2D object.
Hey, I want to make a spawner which is supposed to be a rectangle. I want to make just one rectangle instead of making 15 small circles and then spawn objects on one of them. I want to take a random position which is inside/on the rectangle and then spawn the object in the area inside/on the rectangle like this: 
 
                 
                untitled.png 
                (8.6 kB) 
               
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by mchts · Apr 17, 2019 at 03:00 PM
If you have a RectTransform component on your object you could do this:
 private RectTransform rt;
 
 void Start () {
      rt = GetComponent<RectTransform>();
      //instantiate your dot in the bounds of that recttransform
      for (int i = 0; i < 15; i++) {
             Instantiate(dot, new Vector3(Random.Range(rt.rect.xMin, rt.rect.xMax), 
                   Random.Range(rt.rect.yMin, rt.rect.yMax), 0) + rt.transform.position , Quaternion.identity);
      }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                