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 OmegaNemesis28 · Apr 26, 2014 at 05:12 AM · cameraraycastraycasting

Raycast From Camera to Object

I'm trying to do a raycast from the main camera to an object.

 RaycastHit hitInfo = new RaycastHit();
 Vector3 screenPos = Camera.main.WorldToScreenPoint(gameObject.transform.position);
 Ray rayToPlayerPos = Camera.main.ScreenPointToRay(screenPos);
 int layermask = (int)(1<<8);
 
 if(Physics.Raycast(rayToPlayerPos, out hitInfo, 1000, layermask)) 
 {
     //DO SOMETHING IF HIT, I NEED hitInfo RAYCASTHIT
 }

However this does not work. If I do a

 Debug.DrawRay(rayToPlayerPos.origin, screenPos);

it draws a ray to the right and up of the object. Like completely off. Any ideas? I followed this, or I thought I did anyway: http://answers.unity3d.com/questions/386951/raycasting-from-camera-to-player.html

EDIT: Inversely with a raycast from object to Camera does not work...

 RaycastHit hitInfo;
 Ray rayToCameraPos = new Ray(transform.position, Camera.main.transform.position);
 int layermask = (int)(1<<8);
 if(Physics.Raycast(rayToCameraPos, out hitInfo, 1000, layermask)) 
 {
     Debug.Log ("HIT");
 }
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
1
Best Answer

Answer by robertbu · Apr 26, 2014 at 05:27 AM

Your first the problem with your Debug.DrawRay(). This function needs world coordinates, plus the second parameter is a direction not a position. You are passing a screen coordinate as the second parameter, not a world direction.

As for your Raycast to the camera, there is one issue, and potential issue. First you are passing in two position, Raycast() takes a position and a direction. And Raycasting requires an active collider, so your camera will not have a collider by default. If you are trying to see if something is between a position and the camera, an alternate choice is a Physics.Linecast(). This takes two position. A LineCast() from the position to the camera does not require a collider. If the Linecast() hits anything, then there is an object between the position and the camera. If you wanted to make your current Raycast code from the object to the camera (and you have a collider on the camera), you can do it this way:

 Ray rayToCameraPos = new Ray(transform.position, Camera.main.transform.position-transform.position);
 

As for your Raycast code, this typically how it would be done:

 dir = gameObject.transform.position - camera.main.transform.position;

 if(Physics.Raycast(camera.main.transform.position, dir, out hitInfo, 1000, layermask)) 
 {
     Debug.Log(hitInfo.collider.name+", "+hitInfo.collider.tag);
 }

There is no reason to construct a ray, and therefore there is no reason to convert to screen coordinates to create a ray. And you can construct the direction by subtracting the camera position from the object position.

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 OmegaNemesis28 · Apr 26, 2014 at 07:17 AM 0
Share

Thanks!

What ended up happening was, which I forgot to mention in the question which may have helped, was I have a layered plane over the game between objects and the camera. so what ends up happening is you get a raycast from the object to the camera that gets the intersection of the cast. BUT, the plane was flipped. So when I tried to raycast from the object to the camera it wasn't actually colliding (one sided plane!) Thus, once flipped, it then registered the hit.

Your mentioning of the collider on the camera re$$anonymous$$ded me of the silly mistake.

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

20 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

Related Questions

weird camera preview behaviour 0 Answers

Problem With Raycasts, Interaction Script. 1 Answer

A node in a childnode? 1 Answer

cast a ray to hit an object in the center of the screen 1 Answer

How to make a ray always shoot forward from Camera viewpoint. 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