Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Saucy · Apr 12, 2013 at 08:57 PM · howtouseonbecamevisible

OnBecameVisible - question

This is my current code:

 function OnBecameVisible() {
      Debug.Log("Object Seeennnn!");
 }
 
 function OnBecameInvisible() {
     Debug.Log("Object vision lost");
 }

I want to debug when the camera, or actually the player sees the object this script is attached to.

However, this OnBecameVisible function triggers when the object is in the camera's field of view, regardless of the objects with renderers inbetween. It just looks through them.

I'm keeping in mind that this function triggers all camera's in the scene.

I'm quite lost here and I'd like help to achieve what I'm trying to achieve, and that is: detect if the player/camera actually SEES the object instead of it being in the viewport.

Thanks in advance guys! :)

Comment
Add comment · Show 2
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 justinpatterson · Apr 12, 2013 at 09:08 PM 0
Share

I'm keeping in $$anonymous$$d that this function triggers all camera's in the scene.

For giggles, try turning off / removing all the other cameras besides main camera and see if the issue continues? I just plugged your code into what I'm working on and it seems to work.

Also, this is a silly question but something similar happened to me yesterday :P : what is this script attached to in your code?

avatar image Saucy · Apr 12, 2013 at 09:26 PM 0
Share

It's attached to the object I want to check if seen. In this case a cube for testing purposes. The thing I'm trying to achieve is to check if object is seen, then when the player turns around, the scary character appears and then dissappears. Horror type game :) !

1 Reply

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

Answer by whydoidoit · Apr 12, 2013 at 09:19 PM

So you can't do that with a frustum test - you need to check whether you can see it by raycasting from the camera towards the object - you only need to bother doing that when the object is being rendered by the camera.

   function OnBecameVisible()
   {
         StartCoroutine("CheckForVisible");
   }

   function OnBecameInvisible()
   {
        StopCoroutine("CheckForVisible");
   }

   function CheckForVisible()
   {
        var mainCamera = Camera.main.transform;
        while(true)
        {
              var direction = (transform.position - mainCamera.position);
              var distance = direction.magnitude;
             if(!Physics.Raycast(mainCamera.position, direction, distance - 0.4)) //Or some other factor
             {
                  SendMessage("Seen");
                  return;
             }
             yield WaitForSeconds(0.2);
        }
   }
Comment
Add comment · Show 6 · 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 whydoidoit · Apr 12, 2013 at 09:20 PM 0
Share

This will call the Seen method on other scripts on the same game object when it is seen by the main camera.

avatar image Saucy · Apr 12, 2013 at 09:43 PM 0
Share

Wow, thanks a lot! :) It totally worked except for the yield break part. It kept giving errors. I figured to delete the 'break' and make it yield;. However, this resulted in the function debugging/calling the 'seen' every 0.2 secs. Despite that, amazing work. Thanks! I can figure the rest out (:

avatar image whydoidoit · Apr 12, 2013 at 10:29 PM 0
Share

Ugh - yield break is C# damn it - :S Ok - you could try just to "return" or StopCoroutine("CheckForVisible");

avatar image whydoidoit · Apr 13, 2013 at 07:40 AM 0
Share

Could you mark this question as answered?

avatar image Saucy · Apr 13, 2013 at 08:57 AM 0
Share

Haha yea I was wonderin bout it :). Anyways, sure I can, didnt know I had to. Thanks a bunch.

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

12 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

Related Questions

Can't Understand GetComponent C# 2 Answers

Keep AI @ right height, walk down hills and up and stuff. 3 Answers

How to make a side scroller dirtbike move across hills. 0 Answers

Wait Until Sound is Finished 2 Answers

How to kill enemies by jumping on their heads? 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