- Home /
Object not centering with screen.width/2
void Start () {
Vector3 halfsies = new Vector3(Screen.width/2,gameObject.transform.position.y,gameObject.transform.position.z);
transform.position = halfsies;
}
this is my code that I'm trying to center the gameobject in the center of the x axis of the camera with.
the gameobject's transform starts at (-3.5,5.5,0) it ends with a transform of (297,5.5,0) after the code is executed
the Main Camera has a transform of (0,1,-10)
I don't know why this occurs so any help is useful
Screen.width lives in a 2D screen coordinate system. Your game object lives in a 3D world coordinate system. You need to map between the two when you want to use a screen position for a world object. How you map will depend on your game mechanic. One method that works in many situations is to use Camera.ScreenToWorldPoint() to map between the two. Be sure to set the 'z' parameter of the Vector3 passed to ScreenToWorldPoint() to the distance in front of the camera.
Camera view isn't screen width or screen length. Screen Width and Screen Length is the resolution. This is usually more helpful for creating GUIs.
It's better to manually position game objects into the scene and set them to either active or not active.