- Home /
How Do I Make An Object Appear ONLY When I've Zoomed In Close Enough
Ok, here's my example.
I have a world map with different regions. When the camera is zoomed completely out, the viewer sees only the basic map, however when the user zooms closer, I want the different region names to appear as the camera gets closer.
It's a level of detail issue that I can't quite figure out on my own. I have the region names as objects, is this the best way?
Regards, Jessica
If you're using an orthographic camera and using the size as the zoom, then you could just check when the size is smaller than x and show the objects.
If you only have one camera and it is tagged as "$$anonymous$$ain Camera" then you can use Camera.main to get a reference to it.
Based on the number of differing answers I think we can come to the conclusion that your question is a bit vague. If we have already managed to answer your question then please mark the correct solution as the answer. If not then perhaps posting how your zoo$$anonymous$$g in/out as well as the type of camera might be useful.
Answer by Molehole · May 18, 2015 at 10:45 AM
You could do a script which counts distance between your camera and the object
Vector3.distance(object.transform.position, camera.transform.position);
and then turns rendering on and off depending if the distance is larger than your chosen view distance.
Again, this is only valid if the zoom is made with a movement of the camera (which is not the right way to do but well...who does things right anyway...?)
Answer by fafase · May 18, 2015 at 10:35 AM
http://forum.unity3d.com/threads/this-script-gives-you-objects-screen-size-in-pixels.48966/
float DistanceAndDiameterToPixelSize(float distance, float diameter){
float pixelSize = (diameter * Mathf.Rad2Deg * Screen.height) / (distance * Camera.main.fieldOfView);
return pixelSize;
}
then you can define that if greater than a threshold => render object.
Answer by Cherno · May 18, 2015 at 10:35 AM
You need to keep track of the region name objects, for example by keeping references to them in an array or List of type GameObject. You then need to continually check the distance from the camera's position to each region name object, via Vector3.Distance. If the distance is less than a set threshold, you access that region name object's Renderer component and enable it, otherwise you disable it.
Here is a sample script, it's meant to be attached to the camera gameobject.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TrackDetailObjects : MonoBehaviour {
public List<GameObject> detailObjects = new List<GameObject>();//fill this list via the inspector or via script with the region name objects
public float detailThreshold = 50f;
void Update() {
CheckDistances();
}
private void CheckDistances() {
foreach(GameObject go in detailObjects) {
float distance = Vector3.Distance(transform.position, go.transform.position);
if(distance < detailThreshold) {
Renderer renderer = go.GetComponent<Renderer>();
if(renderer != null && renderer.enabled == false) {
renderer.enabled = true;
}
}
else {
Renderer renderer = go.GetComponent<Renderer>();
if(renderer != null && renderer.enabled == true) {
renderer.enabled = false;
}
}
}
}
}
Note that this is not the most efficient code as it accesses the Renderer component of all objects each frame, but it's good enough to get you started and shouldn't affect performance.
That is only valid if the zoom is a movement of the camera, if the zoom is based on FOV, as should be, this would not work since the distance remains the same.
Ah yes, you are right, I didn't think about the fov. Still, the same theory holds true. Ins$$anonymous$$d of the distance, you compare the fov with the threshold.