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 jvhgamer · Dec 05, 2017 at 10:35 PM · cameracamera-movementfieldofview

IF gameObject is in cameras FOV (rotationally)

Sample image

     // Initialize variables
     private Transform trans;
     private Vector3 screenPoint, cameraRelative;
     private bool onScreen;
     private Camera mainCam;
 
     private void Awake()
     {
         // Assign Transform reference
         trans = gameObject.transform;
 
         // Assign Camera reference
         mainCam = Camera.main;
     }
 
     // Use this for initialization
     void Start()
     {
         // Store initial positions relative to the Camera
         cameraRelative = mainCam.transform.InverseTransformPoint(trans.position);
     }
 
     // Update is called once per frame
     void Update()
     {
         if (trans != null)
         {
             // Convert the position of the object into viewport space. 
             screenPoint = mainCam.WorldToViewportPoint(trans.position);
             // If the viewport space is within 0-1 you are in the cameras frustum
             onScreen = screenPoint.z > 0 && screenPoint.x > 0 &&
                 screenPoint.x < 1 && screenPoint.y > 0 && screenPoint.y < 1;
         }
 
         if (!onScreen)
         {
             trans.position = cameraRelative + mainCam.transform.position;
         }
     }


Walking through this clip, one will see a plane facing toward the camera. When the camera moves positionally so that the plane is not in its viewing frustrum, the plane moves to its original position relative to the camera (acquired at the start using InverseTransformPoint). If one continues watching, when the camera is rotated so that the plane is not in its viewing frustrum, the plane shows little signs of movement, if any. What gives? Code for plane movement provided below. (Camera movement code is irrelevant. Simply using key inputs to adjust position and/or rotation).

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 CardboardComputers · Dec 05, 2017 at 10:45 PM 0
Share

You'd need to rotate the relative position—for example, if the relative position of the object to the camera was at (0, 0, 1), then when the camera rotates down 90 degrees you'd want the object to be at (0, -1, 0) relative to the camera.

1 Reply

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

Answer by Taylor-Libonati · Dec 05, 2017 at 10:45 PM

Well first thing is to narrow the problem. You are doing two things here: Checking if an object is off-screen and then Moving an object. So try logging Debug.Log("OFF SCREEN") on line 36 and see if that is fireing when you rotate the camera. I am guessing it will since the plane seems to move the first time you do that in the gif. So then its a matter of solving your position offset. The reason your position offset doesn't work is because you are storing the original position of the object local to the camera but then you are applying that position back to the object in world space.

 trans.position = cameraRelative + mainCam.transform.position;

Should be something like:

 trans.position = mainCam.transform.TransformPoint(cameraRelative);
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 jvhgamer · Dec 05, 2017 at 10:53 PM 0
Share

Wow. I consider myself somewhat experienced in Unity but anytime I come across transform related calculations I am always at a loss. Sublime work @Taylor-Libonati, this is exactly right. How did you know TransformPoint was the correct method for this functionality? Edit: I see. The answer is in the wording of exactly what you said. Storing the original position of the object local to the camera but, it was then being applied in world space. TransformPoint converts a Vector3 from local to world so the offset can be applied correctly. It should be noted to ensure the object is always facing the user the following line needs to be included after TransformPoint: trans.rotation = mainCam.transform.rotation;

Feel silly for not reversing what is done in Start (InverseTransformPoint applied to mainCam) but then again transform calculation is not my forte

avatar image Taylor-Libonati jvhgamer · Dec 05, 2017 at 11:01 PM 1
Share

Glad I could help! Understanding some Vector / Transform math can help. But the way I think about it is that every Transform in Unity is relative to something. By defualt everything is relative to the root or "World" transform. You stored a position value of object A relative to object B. So if you want to reset A to be where it was you have to move its position relative to B. But transform.position is the world position. So you somehow have to convert your B relative position to world position which is what TransformPoint does. Another solution would be to make A a child of B and then apply your B relative position to its transform.localPosition and then unparent. But that is slower and more steps. Also just another contextual clue for the future is to know that if you convert something with InvertTransformPoint to get it back to where it was you use TransformPoint. Hopefully that helps. It is hard to talk through it text form lol

avatar image Taylor-Libonati jvhgamer · Dec 05, 2017 at 11:02 PM 1
Share

Also I draw everything out on a whiteboard if I need to work with space transformations. Especially if they involve Quaternions.

avatar image jvhgamer Taylor-Libonati · Dec 05, 2017 at 11:08 PM 0
Share

Amazing. I never got to this level of understanding with Vector|Transform|Quaternion math. Nor have I ever used Inverse/TransformPoint before today (that I can remember). Seems I stumble upon these (edit:: these being transform methods I've never heard of before) by the grace of another's knowledge when the implementation desired requires behavior I have yet implemented before. So, thank you very much. I deeply appreciate the wisdom you have instilled in me. Hopefully I can impart this to another in the future.

Show more comments

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

116 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

Related Questions

Enable the OrbitCam script how can I make it will continue from the current camera view and position ? 0 Answers

Mathf.SmoothDamp happens instantly 1 Answer

Smooth camera shift, Lerp? SmoothShift? 2 Answers

Camera gets stuck when cursor is locked 0 Answers

RTS 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