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 Twayne · Dec 08, 2010 at 05:25 AM · camerarayscreenpointtoray

Create a ray from camera to a Vector3 point

I have to create a ray from the main camera through an x,y,z point in space I call guidePos. The language uses screen space but no mouse click is used here. I used -

ray = Camera.main.ScreenPointToRay (Camera.main.WorldToScreenPoint(guidePos));

This works exactly how I want it but is probably very slow due to 2 camera calculations. What is the best way to create this ray? Thank you.

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

Answer by Eric5h5 · Dec 08, 2010 at 06:25 AM

You can do somewhere north of a million ScreenPointToRay+WorldToScreenPoint calculations per second on a half-decent modern desktop CPU. If that's your definition of "very slow", then yes, it is. But the thing that would help you the most is to cache Camera.main in a variable so you aren't retrieving it all the time.

private var cam : Camera;

function Awake () { cam = Camera.main; }

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 Proclyon · Dec 08, 2010 at 08:04 AM 0
Share

Quite so, also quite nice to use when sifting through arrays where the same object is retrieved more than once.

avatar image Twayne · Dec 08, 2010 at 02:35 PM 0
Share

That should be fast enough then. I dont $$anonymous$$d the 2 functions, they are easy to read, just thought I may have missed another function that does this. I added the caching. Thank you.

avatar image
2

Answer by Jesse Anders · Dec 08, 2010 at 05:58 AM

What you have there is probably not in fact 'very slow'. However, if you're just asking how to create a ray given two points 'p1' and 'p2', you can do it like this (untested pseudocode):

rayOrigin = p1;
rayDirection = Normalize(p2 - p1);

The normalization step is optional, strictly speaking (although it's often convenient to have the direction vector be unit length). Also, note that the above may fail if p1 and p2 are coincident or nearly coincident.

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 Robotron18 · Nov 05, 2011 at 08:51 AM 1
Share

That's right. Best cod for end on this question:

     Vector3 camPos = Camera.main.transform.position;
     Ray ray = new Ray(camPos, pos - camPos);

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

1 Person is following this question.

avatar image

Related Questions

How to use ScreenPointToRay for orthographic cameras 1 Answer

Offseting a Ray from camera before casting 1 Answer

Follow mouse cursor (with Y = 0)? 1 Answer

Get 2D Collider with 3D Ray 2 Answers

Why is the camera.screenpointtoray off? 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