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 KrazyKain · Sep 17, 2010 at 09:40 AM · cameraaivisibility

Activate object only if camera views it

In my top scrolling space shooter I'm giving every enemy some sort of script intelligence, but I obviously only want this AI to kick in when they are within view of the camera. Now it is possible to add a timer variable which counts down and then manually alter it to match the rate at which the camera will reach the enemy, but that feels very inefficient. Is there a good way to get a boolean that turns true as soon as the camera sees the object?

Comment
Add comment
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

3 Replies

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

Answer by jashan · Sep 17, 2010 at 09:52 AM

You're looking for Renderer.isVisible

if (renderer.isVisible) {
   DoSomeComplexEffect();
}

What you'd probably do is: In every update (or every few updates, could be also controlled via a Coroutine), check whether the state of renderer.isVisible has changed (went from false to true, or vice versa). When the object became visible, start your AI.

Or, you could have your AI code in the if:

if (renderer.isVisible) {
   HandleAIStuff();
}

Depends much on how your AI is exactly implemented.

There's one thing to be aware of, though: isVisible will also be true if the shadow of the object is visible (which sometimes is the case even though you wouldn't expect it). So watch out with this, if you're using shadows.

Oh, and of course: any camera (including scene view cameras in the editor). So that also requires be a little careful when testing this.

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 KrazyKain · Sep 17, 2010 at 11:02 AM 0
Share

Sir, I think I love you :)

hehe thanks man, it worked perfectly, couldnt beleive the command was so simple

avatar image
1

Answer by Bampf · Sep 17, 2010 at 10:54 AM

Other good answers to this question can be found here. In particular, if you have multiple cameras you can use OnWillRenderObject instead.

Another way not mentioned is to use raycasting. That would be the way to go if you want to account for obstacles blocking the view of the camera/enemy. Here is an answer on that topic.

Comment
Add comment · 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
0

Answer by bliztertoto · Dec 18, 2013 at 06:36 PM

If you want to do it without any renderer:

Calculate the vector between your camera and your object then Dot your camera.transform.forward with the difference vector. If they are in the same direction, then the object is in front of the camera.

Then using WorldPointToScreenPoint, make sure the object is within the viewport rect.

Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

How to change camera visibility after certain distance? 0 Answers

How to get an AI enemy character to stop moving and freeze animation when the FPS camera is looking at it? 1 Answer

Can't see a flare from above? 0 Answers

light sound sensitive AI 1 Answer

How to check that a game object is shown on screen? 0 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