Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

c# - Make object visual size constant despite the distance

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Not really sure what you are doing wrong, as dividing distance by constant must give required effect. Anyway try this code:

using UnityEngine;

public class AutoSize : MonoBehaviour
{
    public float FixedSize = .005f;
    public Camera Camera;

    void Update ()
    {
        var distance = (Camera.transform.position - transform.position).magnitude;
        var size = distance * FixedSize * Camera.fieldOfView;
        transform.localScale = Vector3.one * size;
        transform.forward = transform.position - Camera.transform.position;
    }
}

It seems to work fine - preview

Also maybe it will be more effective to create constant menu size and just position it by converting world space to the screen space.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...