Dynamically adjust any plane infront of Camera
Hello everyone,
I'm working on an algorithm that would place a game object infront of the Camera, the game object would be adjusted in a way that it fits in the camera point of view regardless of its size.
The way I tried to do it is by increasing the distance between the game object and the camera if the game object is large and decreasing it if the game object is small. My code works well with all types of game objects with various scales however, when it comes to planes, it just doesn't.
I did my research and found out that the planes are supposed to be scaled x10 of a cube for example. I tried to include this factor in my code but the results were inconsistent and dependable on the size of the plane. If the plane is too large it would not fit into the field of view and if the scale is too small it would fit but wouldn't fill the field of view.
This is the cod I wrote:
public void moveObject(){
float screenWidth = Screen.width;
float screenHeight = Screen.height;
float objectWidth = gameObject.transform.localScale.x;
float objectHeight = gameObject.transform.localScale.y;
var frustumWidth = 0f;
var frustumHeight = 0f;
var distance = 0f;
if (objectHeight / objectWidth > screenHeight / screenWidth) {
frustumWidth = ((gameObject.transform.localScale.y*10)+30) * mainCamera.camera.aspect;
//the *10 in the above is where I factored the scale of the plane
frustumHeight = frustumWidth / mainCamera.camera.aspect;
Debug.Log("do height");
} else {
Debug.Log("do width");
frustumHeight = ((gameObject.transform.localScale.x*10)+20) / mainCamera.camera.aspect;
//the *10 in the above is where I factored the scale of the plane
}
distance = frustumHeight * 0.5f / Mathf.Tan(mainCamera.camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
Debug.Log ("Distance is " + distance);
gameObject.transform.localRotation = mainCamera.transform.localRotation;
gameObject.transform.SetParent (mainCamera.transform);
gameObject.transform.localPosition = new Vector3(0 , 0, distance);
}
The above code isn't giving me the right distance so the plane would fit regardless its scale. The code works well (without the factoring) for cubes and other types of gameobjects so I feel like its a small thing I'm missing, I would appreciate it if someone could take a look at that.
Thanks everyone.
And I immediately found the answer lol
what made the results inconsistant was the (30) and the(20) that I added as padding, logically if the distance between the camera and the plane is large, the padding will seem smaller because it is a constant value, and if the the distance is small the padding will seem larger as well.
So I left the question and as soon as it is submitted I will add the answer for anyone who would like to use the code. its fairly simple.
Thanks.
Your answer
Follow this Question
Related Questions
Confine mouse to a plane 1 Answer
I can't fit a plane to my field of view 2 Answers
camera force scaling child objects SOLVED 1 Answer
Changing Camera Field of View 0 Answers
changing the scale of camera in VR 0 Answers