- Home /
Permanent space between objects
I try place 10 cubes side by side with space like on image
but i have problem – some time space between objects looks different.
My code for x cube position
float lastX = startPoint;
float offsetBetweanCubes = 0.3f;
for(int i=0; i < 10; i++) {
lastX += gameObj.GetComponent<MeshFilter>().mesh.bounds.size.x + offsetBetweanCubes;
Instantiate(gameObj, new Vector3(lastX, lastY, startPoint.z), Quaternion.identity)
}
is it way to fix this?
Answer by Dracorat · Nov 12, 2013 at 04:43 PM
The spacing is the same - what you're seeing is the result of being zoomed out and having geometry not perfectly aligned to pixel edges.
You need to adjust the zoom of your camera to get the spaces to all appear the same. Also, if you maintain the same zoom at different resolutions, each resolution will appear different. If you can fix it for one resolution, then you need to maintain the same pixel ratio for geometry in other resolutions - by increasing or decreasing how much is on-screen (instead of scaling the scene according to resolution)
You are right. When i fix screen aspect to 4:3 or 3:2 grid displayed right. Thank you.
Answer by Triqy · Nov 10, 2013 at 07:46 PM
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Transform prefab;
void Example() {
int i = 0;
while (i < 10) {
Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity) as Transform;
i++;
}
}
}
Thank you. I try this but have error on this line:
Instantiate(gameObj, new Vector3(i * 4.5F, 0, 0), Quaternion.identity) as Transform;
Only assignment, call, increment, decrement, and new object expressions can be used as a statement
if i delete "as Transform" compilation success. But problem not solved.
space between objects still different.
Note: i use camera with orthographic projection.