How to spread out points equally across the screen width?

Updated on March 9, 2018 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on March 9, 2018

This should be a very simple math problem, but I’m having trouble getting some placeholder images to spread out equally all the way across the bottom of my screen. I thought “width/number of items = items placed across” was the basic answer, but that doesn’t get exactly what I want. 

Here’s what is happening. I’d like all the items to spread out equally between the red lines, essentially.


Here’s the code: 

Camera cam;
 public Transform InventorySlot;
 GameObject[] InventoryArray;
 GameObject PosBooth;
 void Start () {
 cam = Camera.main;
 PosBooth = GameObject.Find("PosBooth");
 InventoryArray = new GameObject[5];
 for (int i = 0; i<InventoryArray.Length; i++)
 Instantiate(InventorySlot, cam.ScreenToWorldPoint(new Vector3(Screen.width/InventoryArray.Length *i , 0)), InventorySlot.rotation);

I’d appreciate help, thanks.


  • Liked by
1 on March 9, 2018

You just need to add an offset to nudge them a bit towards the center.

The offset would just be 1/2 of the spacing that you already get from segmenting the screen width by the number of elements.

Your new instantiation position would look something like this.

float xOffset = Screen.width/InventoryArray.Length;
Vector3 spawnPos = new Vector3((xOffset * i) + (xOffset * 0.5f), 0, 0);
Instantiate(InventorySlot, cam.ScreenToWorldPoint(spawnPos), InventorySlot.rotation);


on March 9, 2018

Thank you, I’ll try this tomorrow evening. 🙂

Show more replies
  • Liked by