Hi all. I have some 3d objects that I spawn in a plane area, like this:
foreach (GameObject item in movingPlayers)
item.transform.position = new Vector3(Random.Range(-145f, 145f), 2.6f, Random.Range(-90f, 90f));
The Vector3 is the dimension of the plane in my world.
I need to spawn this objects only in the plane area seems by the camera (cause camera keeps moving/rotating).
Any ideas? Thanks
Maybe there is another better option, but you have this fucntion:
To know if some object is visible by the camera. You can use it to know where to spawn.
You have also this function:
To change from screeen point to world coords. It can also be used for your propuse.
Hey, @tormentoarmagedoom , thanks for take your time to respond
I tried using ScreenToWorldPoint, but didn’t worked.
I solved dividing the plane in small portions where the camera was passing. A dirt job, but worked hehe
The dirt soultion I’ve founded didn’t pleased the people that work with me. So, a friend of mine explained to me this ScreenToWorldPoint thing. I’ll put the solutin here, so it can help someone in need (I’ve spended 3 days in this)
//This is the object that I want to spawn and appear in the camera
Vector3 ballPosition = new Vector3(Random.Range(-130f, 130f), Random.Range(0.3f, 5f), Random.Range(-80f, 80f));
//This convert the position of my object, to viewport coordinates
Vector3 viewPos = Camera.main.WorldToViewportPoint(ballPosition);
//Here I check if my object is in viewport position (between x 0 - 1 and y 0 - 1
if (viewPos.x >= 0f && viewPos.x <= 1f || viewPos.z >= 0f && viewPos.z <= 1f)
movingBall.transform.position = ballPosition;
Debug.Log("Positions are ok, moving the ball");
//Here the object is moved again if not in the viewport area
else ballPosition = new Vector3(Random.Range(-130f, 130f), Random.Range(0.3f, 5f), Random.Range(-80f, 80f)); ;