• 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 Eiskaltnz · May 22, 2012 at 07:04 AM · cameraraycastscreencrosshair

Raycast based on crosshair on screen

Hello

I have a camera (that moves by itself) w$$anonymous$$ch has a crosshair that can move freely around the screen. For the basic idea you can t$$anonymous$$nk of Time Crisis or games like that.

I want to be able to raycast from the camera to the object that the crosshair is over. Basically a raycast with the origin of the destination and the correct angle to make sure it $$anonymous$$ts what the crosshair is pointing at. Though it can be done by moving the origin and having a it go straight.

I want the raycast angle to change when the crosshair moves around the screen.

I have tried camera.ScreenPointToRay(new Vector3(xPos, yPos) with xPos and yPos being the positions of the crosshair on the screen.

I have also tried ScreenToWorldPoint and ScreenToViewpointPoint all with limited success.

If anyone has any suggestions or can help at all it would be appreciated.

Thanks.

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

Answer by amirabiri · May 22, 2012 at 08:26 AM

Camera.ScreenPointToRay is the answer, so you should double check the rest of your code. Remember that ray casts only $$anonymous$$t colliders, check distances, coordinates, etc. If the crosshair follows the mouse, you could use Input.mousePosition.

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 Eiskaltnz · May 22, 2012 at 08:50 AM 0
Share

Thanks. I got it working but I had to detach the camera from my character.

I have the camera attached to the character's bone so it moves along with the animations and this makes the rays go in totally the wrong direction.

Any ideas?

avatar image amirabiri · May 22, 2012 at 09:38 AM 1
Share

The ray will always genuinely represent the screen point in the world in the current frame. The question is what are you trying to achieve?

If you want the camera to move around with the movement of the character then the different ray casts represent the result of this effect.

If you just want to achieve a simple "head bob" effect without the complexity of putting the camera on a bone then consider making a simple script that jigs the camera around a certain point depending on the movement speed of the character.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to engage an object if it's seen by my camera?? 1 Answer

Question about sending physics raycast from the camera 1 Answer

Camera Point-and-click Movement 2 Answers

Detect touch in screen at different resolutions 2 Answers

Picking up things for your inventory... 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