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
0
Question by N00MKRAD · Jul 22, 2014 at 04:04 PM · objectvisible

[Solved] Detect if object is visible and do something if its visible?

(Sorry, maybe my English isn´t perfect)

Hi, I want to play a sound or enable a gameObject if something (a NPC) is visible (In a range), but I have no idea how to do that.

For example: If I see a zombie, which is closer than e.g. 20m a text will appear, and if he isnt visible anymore, the gameObject will be disabled again.

Thank you!

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 Scribe · Jul 22, 2014 at 04:14 PM 0
Share

You can use Renderer.isVisible

And useful stuff includes the methods OnBecameVisible and OnBecameInvisible

avatar image robertbu · Jul 22, 2014 at 04:16 PM 0
Share

You need to be clear about what you want. Distance calculations and vision calculations are not the same. Also you need to specify if you are talking about being visible to the camera (i.e. first person), or visible to another object (i.e. third person). You also need to specify if you want to detect when one object is hiding behind another.

http://answers.unity3d.com/questions/8003/how-can-i-know-if-a-gameobject-is-seen-by-a-partic.html

http://docs.unity3d.com/ScriptReference/Vector3.Distance.html

http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

1 Reply

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

Answer by SirCrazyNugget · Jul 22, 2014 at 04:27 PM

If you want this to be able to be enabled and disabled you'll need to place a second script which handles this as once you've disabled it Update will no longer run.

 void Update(){
   if(renderer.isVisible && Vector3.Distance(zombie.position, player.position) <= 20f){
     showText = true;
   }else{
     showText = false;
   }
 }
 
 void OnGUI(){
   if(showText){
     GUI.Label("Zombie")
   }
 }


You could use void OnBecameVisible(){} and OnBecauseInVisible(){} to add and remove a delegate function instead of using Update to remove the small overhead though may be more trouble than it's worth.

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 tall-josh · Mar 22, 2017 at 10:08 AM 0
Share

This has the same problem as every other post I have seen so far to do with the visibility of Objects.isVisable() is dependent on all cameras rendering an object. If the object is visible from the editor window it will return true.

What about just the player view???

[Edit] I figured out a solution that worked for me. I posted the answer here:

How can I know if a gameObject is 'seen' by a particular camera?

The leading answer from @duck points you in the right direction, but it you want code have a look for my offering. I hope it helps.

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

24 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

Related Questions

Make object visible only for camera "X" 1 Answer

Fade Object left to right 1 Answer

Finding point of mesh visible by another game object 1 Answer

Making 3D Objects invisible 1 Answer

Know if gameObject is being seen 2 Answers


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