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
2
Question by dhendrix · Mar 29, 2010 at 12:56 AM · raycastpositionview

Getting position where raycast goes offscreen

Is it possible to fire a raycast in a direction, then get the position when the raycast hit the edge of the screen in world coordinates? For example -

alt text

Let's say this is a 2d game and the black circle is the player. He fires off raycasts in arbitrary directions (the blue rays). Can we get the position where the ray left the camera viewport? Apologies if I'm missing something simple here(which is likely).

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
6
Best Answer

Answer by Murcho · Mar 29, 2010 at 04:29 AM

You can get the Camera Frustum planes by using the GeometryUtility.CalculateFrustumPlanes() function built into Unity. This function returns an Array of planes that make up the viewport. You can then use the algorithms on this page to determine where the intersection happened.

This should be faster to calculate than using colliders, and doesn't need to involve the physics engine at all.

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 dhendrix · Mar 29, 2010 at 05:22 AM 0
Share

Was not aware of that function, thanks for the info!

avatar image
1

Answer by e-bonneville · Mar 29, 2010 at 01:20 AM

I'm not exactly sure what you're asking. However, it seems to me that you want to find the position that the players bullet left the area you can view. One way to accomplish this is to create four box colliders. Arrange them around the edges of the screen and make them children of the camera. Then, using Raycast.Hit, you can find the positions at which they 'left the screen'. In other words, where they hit the colliders arranged around the edges of the screen. You can then use hit.point like this:

var hit : RaycastHit; var edgeOfScreenHit;

function Update () { if (Physics.Raycast (ray, hit)) { edgeOfScreenHit = hit.point;
} }

Hope this helps!

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 dhendrix · Mar 29, 2010 at 01:51 AM 0
Share

Well yes, you got the point of my question. I was throwing around the idea of doing colliders as you suggest, problem is it seems like a pain scaling them when the camera zooms in and out.

avatar image e-bonneville · Mar 29, 2010 at 02:10 AM 0
Share

Ah, you're doing zoo$$anonymous$$g... I was worried about that. Try putting them all in a single GameObject, then scaling that in proportion to the cameras field of view.

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

No one has followed this question yet.

Related Questions

Raycast from mouse position in editor scene view? 1 Answer

Camera rotation around player while following. 6 Answers

Get Vertice of a Quad (two tris) via raycast? 0 Answers

Dynamically Change Position of RayCast 1 Answer

transform position player teleport 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