I am trying to spawn enemies on top of the screen, currently I am using simple Random.range(-3,3) for positions of the enemies. However I will probably want to release my app/game for multiple devices (device resolutions, aspect ratios etc), so I want the spawn point of the enemies to be random on x axis and I want it to be dependent of the screen resolution.
I’ve already did player movement and I used Camera.main.WorldToScreenPoint(transform.position) but I am struggling with the random (screen res dependent)spawn point for enemies.
Could you guys help me a bit with this please? I’m trying not to worry about this and carry on but its stuck in my head hehe
I am using c#
English is not my first language so I hope I did explained my problem well, if not I apologize.
Thanks, that’s what I thought in the first place, however instead of placing object to the screen it places object way off the screen. To be honest It’s looks like unity is puts values of current screen resolution into transform.position values of the object which moves object away from the camera view.
Instantiate (enemyCubeBig,new Vector3(Screen.width/1,0,0),Quaternion.identity); //I’ve tried this
Instantiate (enemyCubeBig,new Vector3(Screen.width*0.1f,0,0),Quaternion.identity); //I’ve tried this
You have to use 2d vector and then cast a ray to the 3d position with a ray:
That example uses input.mousePos, but you would replace it with random screen width and screen height as a 3d vector, I think they zero out the z, but you’d have to check that. Just get a mouse position from input.