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 votaxinor · Nov 30, 2019 at 12:31 AM · cameraplayerprogrammingnotlook at

How would I check if the player camera is looking away from an object?

I've been trying to figure out how one would figure out how to register when the object leaves the camera's view (Like a horror game where when you look away, an event with audio triggers). Although I'm unsure how to go about it. I got two options that I know of, and I'd like to know how others approach this as I'm dead certain that I'm going about it incorrectly.

Option 1: Raycast and register on walls/objects which are known to be out of sight(This would mean adjusting all the walls in a general radius to contain certain tags for every event, or scripts..., or layers...)

Option 2: Calculate the player cameras rotation where the center would be placed on the object(Every update since when the player moves around the calculation would change), then compare it to the player's actual rotation and when the player rotation goes out of pre-calculated bounds(Based off of the players distance + resolution? Resolution affects how wide or tall the camera is so...) it'd trigger the event.

Are there more practical options than these?

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

2 Replies

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

Answer by Ermiq · Nov 30, 2019 at 01:23 PM

Well, simplest way is to check the angle between camera forward vector and the vector that is the direction from camera to some position. And use camera FOV as a threshold. If the angle between is more than camera FOV, than object might be out of the player's view.

 Transform objectThatShouldBeOutOfSight;
 Vector3 directionToObject;
 
 directionToObject = objectThatShouldBeOutOfSight.position - camera.transform.position;
 
 if (Vector3.Angle(camera.transform.forward, directionToObject) > camera.fieldOfView)
     {
         ScaryBoo();
     }
Comment
Add comment · Show 2 · 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 votaxinor · Nov 30, 2019 at 07:19 PM 0
Share

I just tested your answer, and it seems to work with different aspects and it is certainly better than what I was going to do. Thank you! The only gripe I have is that it registers the object being out of sight a bit further off the actual screen than the play screen, although that can give it a bit of a delay so this is very likely the option I'm gonna go with.

avatar image Ermiq votaxinor · Dec 01, 2019 at 04:59 AM 0
Share

Yeah, it's because when the camera FOV is, let's say, 90, then the object should be at 90 degrees off the camera line of sight. Actually, I should have wrote camera.filedOfView / 2f in the code. But it will eli$$anonymous$$ate the delay then, so you probably would better use camera.fieldOfView * 1.5f to have some delay.

avatar image
2

Answer by ralpholiver · Nov 30, 2019 at 06:17 AM

There is an event called OnBecameInvisible which probably solves your issue. However, this might trigger too early. Consider a perfectly oriented north player, aligned with the said interest object. Depending on the size of the target, by the time you rotate around 45 degrees heading east, it would already trigger, when a perfect "away" situation would be at 180 degrees. If this bothers you, you can combine the event with your second approach, customizing and determining the boundaries where your action will take place, and checking only when the object is not on screen.

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 votaxinor · Nov 30, 2019 at 07:35 PM 0
Share

I like this option, it's nice and simple. The only issue is once the camera goes a certain distance down, it registers the object as seen in most situations. This works very well with out much input, although I think there's a good option here: I'm gonna mix this answer and the one made by Ermiq for the most effective but also efficient way (As you stated at the bottom).

When it triggers that the object is invisible then it works fine (I'm just looking for it to trigger on things like light bulbs and mannequins so nothing big to bug it out).

When it triggers that the object is visible then it will do the check Ermiq offers (Changed a little bit as it's on the object itself) to deter$$anonymous$$e if it really is invisible just being dumb.

This should work, thanks guys!

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

212 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 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

Multiple Cars not working 1 Answer

How to make camera not rotate with player, only move 1 Answer

Networked Game Issues - Camera does not follow script 1 Answer

Aimming zoom problem 0 Answers

C# - Offset In-Air Camera from Current Object Position 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