Hi. I have problem with new object in scene. Im setting it position, local, normal any type. I set x and y position on Screen.width and Screen.heigth. Problem is when I instatiate this objects in another resolutions. Once is over the screen, another time is on the half of screen
I dont understand how it works. I want have object in the same point in any resolution.
flameFloor.transform.position = new Vector3(Screen.width, Screen.height, flameFloor.transform.position.z);
Instantiate(flameFloor);
Game is in 2D
The key is Camera.ScreenToWorldPoint
Since the value of the resolution changes, you will need to normalize your screen position and deal in percentages not pixels.
Here is a functional Demo:
public class Example : MonoBehaviour {
public GameObject yourPrefab;
[Range(0.0f,1.0f)]
public float xPercentageOfScreen;
[Range(0.0f,1.0f)]
public float yPercentageOfScreen;
public float distanceFromTheMainCamera;
void Update(){
if(Input.GetKey("i")){
InstantiateGOAtPoint();
}
}
void InstantiateGOAtPoint(){
if(yourPrefab != null){
Instantiate(yourPrefab,PositionOfInstantiateGameObject(),Quaternion.identity);
}
}
Vector3 PositionOfInstantiateGameObject () {
float xConvertedToCurrentResolution = xPercentageOfScreen * Screen.width;
float yConvertedToCurrentResolution = yPercentageOfScreen * Screen.height;
Vector3 screenCoordinates = new Vector3(xConvertedToCurrentResolution,yConvertedToCurrentResolution,distanceFromTheMainCamera);
return Camera.main.ScreenToWorldPoint(screenCoordinates);
}
}
Note that (0,0) is in the bottom left corner of the screen and (1,1) is in the top right.
Hope that helps!
Great thank, now works great! 