Visual aid here
Please see visual aid. Following the gif, there are two GUIs. The GUI on the left is scaling properly based on the cameras distance to the object. The GUI on the right is scaling, seen clearly in the 'Scene' view, but not even noticeable in game view. The application is for the Hololens as the users head controls the camera within the Unity scene.
The problem lies in setting the objects scaling. Been racking my brain all day trying to replicate the type of scaling shown on the left GUI. Dividing the distance and using this as the factor to scale by is clearly incorrect as the object is either far too large and grows too fast, or far too small and grows too slowly but I don't know how to meet somewhere in the middle. Scaling in both directions equally at a noticeable yet not obnoxiously large or imperceptible rate.
Implementation code thus far:
// GameObject and GUI assigned in Editor
public GameObject sphere, rotationManager;
// For rotationManager scaling
private float cameraToObject,
void Update()
{
/* Scale rotationManager based on Cameras distance to object */
cameraToObject = Vector3.Distance(
sphere.transform.position,
Camera.main.transform.position);
Debug.Log("distance: " + cameraToObject);
// If camera isn't too close to our object
if (cameraToObject > 4f)
{
// adjust scale
rotationManager.transform.localScale = new Vector3(
cameraToObject / 5,
cameraToObject / 5,
rotationManager.localScale.z);
// enable rotationManager
rotationManager.gameObject.SetActive(true);
}
// Camera is too close, turn off GUI
else
rotationManager.gameObject.SetActive(false);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…