Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FreakyDevA · May 22, 2018 at 07:23 AM · cameraunityeditordistance

View the distance an object has from the camera

Can I view the distance an object has from the main camera through the Unity editor UI somewhere?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Harinezumi · May 22, 2018 at 08:02 AM 1
Share

I don't think that by default there is a feature in Unity for this, however, it should not be difficult to write an editor script that does this. I will try to come up with one quickly.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Harinezumi · May 22, 2018 at 09:16 AM

A different solution using EditorWindow (need to have the custom editor window open for it to work):

 using UnityEngine;
 using UnityEditor;
 
 public class DistanceFromCamera : EditorWindow {
 
     private GUIStyle style;
 
     [MenuItem("Tools/Distance window")]
     private static void Init () {
         DistanceFromCamera window = GetWindow<DistanceFromCamera>();
         SceneView.onSceneGUIDelegate += window.OnSceneGUI;
         window.Setup();
     }
 
     private void Setup () {
         style = new GUIStyle();
         style.normal.textColor = Color.red;
         style.fontSize = 16;
         style.alignment = TextAnchor.MiddleCenter;
     }
 
     private void OnDestroy () {
         SceneView.onSceneGUIDelegate -= OnSceneGUI;
     }
 
     private void OnSceneGUI (SceneView sceneView) {
         string distanceString = "N/A";
         Camera mainCamera = Camera.main;
         if (mainCamera != null) {
             GameObject[] selectedObjects = Selection.gameObjects;
             if (selectedObjects.Length == 1) {
                 GameObject selectedObject = selectedObjects[0];
                 float distance = (selectedObject.transform.position - mainCamera.transform.position).magnitude;
                 distanceString = distance.ToString();
             }
         }
         Handles.Label(Selection.gameObjects[0].transform.position, distanceString, style);
     }
 
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FreakyDevA · May 22, 2018 at 09:41 AM 1
Share

Thank you very much!

avatar image
1

Answer by Hellium · May 22, 2018 at 08:14 AM

Here is a custom editor script drawing a line between the currently selected object and the camera. Place this script in an Editor folder.

 using UnityEngine;
 using UnityEditor;
 
 [CustomEditor(typeof(GameObject))]
 public class DistanceFromCameraEditor : Editor
 {
     [DrawGizmo(GizmoType.Selected)]
     static void RenderCustomGizmo(Transform objectTransform, GizmoType gizmoType)
     {
         Camera camera = Camera.main ;
         
         if( camera == null )
             camera = FindObjectOfType<Camera>();
         
         if( camera == null )
             return ;
         
         Vector3 midPoint = (objectTransform.position + camera.transform.position) * 0.5f ;
         float distance = Vector3.Distance( objectTransform.position, camera.transform.position );
         Gizmos.DrawLine(objectTransform.position, camera.transform.position);
         Handles.Label(midPoint, distance.ToString());
     }
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FreakyDevA · May 22, 2018 at 08:36 AM 0
Share

Thank you kindly it works great!

avatar image Harinezumi · May 22, 2018 at 09:47 AM 1
Share

For me this script changes how the inspector looks (can't upload files for some reason, so here is a link): https://ibb.co/kAN$$anonymous$$$$anonymous$$T .
Do you know why this happens? I tried this in Unity 2017.3.1f1.

avatar image FreakyDevA Harinezumi · May 22, 2018 at 10:05 AM 0
Share

You are right I hadn't noticed this. It also happens to me in Unity 2018.1.0b13. I have no idea why.

avatar image Hellium Harinezumi · May 22, 2018 at 10:23 AM 1
Share

Did not notice that, indeed.

Because there already is a custom inspector of the GameObject type, this class overrides the custom inspector. And it seems, it's not possible to inherit from this class defined as internal....

You should go with @Harinezumi 's solution

avatar image Hellium Hellium · May 22, 2018 at 10:30 AM 0
Share

Identical problem here: https://answers.unity.com/questions/343984/custom-editor-makes-inspector-view-act-weirdly.html

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera Distance 1 Answer

camera zoom in/out position based on object's speed 1 Answer

How to translate a camera forward and back(basically a zoom) according to the distance of two objects? 1 Answer

Ray Cast not working for camera. 1 Answer

Calculate distance between AR object and camera 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges