How to spawn an object inside the camera's view

The title is pretty hard to understand so I’ll go into more detail of what I’m trying to do. So what I want is for the object to spawn randomly within the camera’s view. The problem I’m having is that I can’t figure out how to set the area the vector can be at inside the camera based on the pixels because I need those to offset the object to make sure it’s not half way in the camera’s view. (right now I’m not offsetting for the object size because I haven’t created them yet, I’m just using circles and cubes)

Here is what I have and if you could explain my problem that would be great:

	public Vector2 randomVector2()
	{
		float camWidth = Camera.main.pixelWidth;
		float camHeight = Camera.main.pixelHeight;
		float camX = Camera.main.WorldToScreenPoint (Camera.main.transform.position).x;
		float camY = Camera.main.WorldToScreenPoint (Camera.main.transform.position).y;

		Debug.Log ("Cam X " + camX);
		Debug.Log ("Cam Y " + camY);

		float x = Random.Range (camX - (camWidth / 2), camX + (camWidth / 2));
		float y = Random.Range (camY - (camHeight / 2), camX + (camWidth / 2));

		return new Vector2(x, y);
	}

How this problem is solved, as well as how easy or hard this problem is to solve, is based on a number of factors:

  • Is the camera orthographic or perspective?
  • Are the object all on a plane parallel to the camera plane or at random distances from the camera?
  • Is it important to snug objects to the screen edges, or is a little slop okay?
  • Do the objects vary in size or are the similar in size?

If the objects are similar in size, on a plane parallel to the camera plane, and you can live with a bit of slop, you can solve the problem using Viewport coordinates. Viewport coordinates go from (0,0) in the lower left of the screen to (1,1) in the upper right. The code:

float x = Random.Range(0.05f, 0.95f);
float y = Random.Range(0.05f, 0.95f);
Vector3 pos = new Vector3(x, y, 10.0f);
pos = Camera.main.ViewportToWorldPoint(pos);

This code creates a point 10 units from the camera plane and it will be 5% away from all the edges.

You should be able to achieve that using Camera.pixelHeight and Camera.pixelWidth.
These two are read-only properties of the Camera class and they return the size in pixels of the viewport. You can then get a random value between 0 and pixelWidth and convert that to worldpoint (or better use a raycast and spawn the object on the hit point, but that’s another topic).

I’ve never done this myself, so I might be wrong here. But that’s how I would try it. (Can’t do that now sadly)

If my camera is not parallel to the game object then how to calculate the z axis . My camera is at angle

@Kobting
Pick a random z distance from the camera, then get the transform.forward of the camera, create max height and width variables and use pythag to find their values:

`
height = transform.forward.magnitude / Mathf.Cos(verticalFOV / 2)

width = transform.forward.magnitude / Mathf.Cos(verticalFOV / 2)
`

Make sure to be in correct degrees / radians!
Then pick a random value x, y value between the bounds and create a position vector that is:

tranform.position + transform.forward * randomZ + transform.up * randomY + transform.right * randomX