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 boxing_rex · Oct 28, 2014 at 11:48 AM · c#cameraraycast

Raycast in camera

How would one go about casting lots of rays inside of the cameras bound, plus a little extra on each sides ?

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

1 Reply

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

Answer by Yokimato · Oct 28, 2014 at 12:37 PM

Might need a bit more information to get you the right answer, but I can give it a shot with what you got. Reading a bit into the missing pieces I'm guessing you're trying to check if something can be seen from your camera.. below is various ways to check:

The important vector for Rays from the camera is direction. You can get access to this from the transform: camera.transform.forward. From there, you can cast a ray in that direction from the camera's position for as far as you want.

Now, if you've got a target you're trying to see if you can hit.. you can do this more easily, by ditching the direction and using the target's position to figure out the direction vectory like so:

 RaycastHit hit;
 float distanceToCheck = 100f;
 
 if (Physics.Raycast(camera.transform.position, (target.transform.position - camera.transform.position).normalized, out hit, distanceToCheck) {
     // can check what hit landed on
 }


Further though, if you wanted to check if your target was in the frustum of the camera you can do so like:

 Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
 
 if (GeometryUtility.TestPlanesAABB(planes, target.renderer.bounds)) {
     // target is in the camera frustum...
 }

Hope this gives you enough to get you objective done. If not, provide some further information and I can edit if necessary.

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 boxing_rex · Oct 28, 2014 at 12:43 PM 0
Share

Just for some further information, I am going to make a custom occlusion culling system. Sorry for the lack of information but I was on my work break, didn't have much time. Thanks for the answer, I will try it when I get home.

avatar image boxing_rex · Oct 28, 2014 at 01:04 PM 0
Share

Is there a way to use the frustum planes to cast rays, because I need to detect if the object can be hit by the ray, if it can then render it else don't. There is no point rendering something that is behind another object, so I don't think the testPlanesAABB will work for this.

avatar image mattyman174 · Oct 28, 2014 at 09:39 PM 0
Share

If the target is inside the camera view frustum, then simply fire a ray at its location to deter$$anonymous$$e if it is behind something. The hit result will tell you what GameObject was hit, if it is not your target then your target is being occluded.

avatar image Yokimato · Oct 28, 2014 at 11:02 PM 0
Share

Precisely. First check if it's even in frustum then if so cast a ray to see if it's visible

avatar image boxing_rex · Oct 29, 2014 at 08:59 AM 0
Share

How would I replace target.renderer.enabled with world objects on the go, because I need it to be automatically done, so I wont know what the target is.

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

27 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

Related Questions

Mouse pos + raycast 1 Answer

How to make a ball stay in the air longer when force is added? 1 Answer

Working with RayCast 1 Answer

Changing a Variable within the same script 0 Answers

Using raycast from camera to aim shots 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