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 AndreasX12 · Oct 14, 2013 at 09:59 AM · raycastraycasthitcrosshaircenter

Find gameobjects in a crosshair?

Hi, I have a gameobject attached 3 units in front of my plane. I convert the wolrd space to a gui rect and then I draw the gui. I'm trying to figure out how to check if there's an enemy within the crosshair rect. I have tried with RaycastHit, but since the gui is not always in the center of the screen, that's very difficult.

alt text

What would be the best way to see what gameobject is within the gui rect?

Comment
Add comment · Show 8
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 Crystalline · Oct 14, 2013 at 10:10 AM 0
Share

I think you should ray cast from the crosshair ins$$anonymous$$d ,not the center of camera .

avatar image AndreasX12 · Oct 14, 2013 at 10:12 AM 0
Share

That's also what I've done, but I can't find out which angle I should be using since transform.forward (Vector3.forward) isn't the right one :-)

avatar image ChaosFollowing · Oct 14, 2013 at 10:22 AM 0
Share

To clarify, the correct enemy target should be visible in the crosshair, from the perspective of the camera? (and not from the plane?)

avatar image Crystalline ChaosFollowing · Oct 14, 2013 at 10:28 AM 1
Share

$$anonymous$$ake sure in the future not to post questions as answers, use comments ins$$anonymous$$d.

avatar image AndreasX12 ChaosFollowing · Oct 14, 2013 at 10:28 AM 0
Share

When you control the plane, you should position it so that the crosshair is "containing" the enemy (for example, another plane). All I need is just a way to raycast from the crosshair gameobject, so that the raycast goes through the gameobject(enemy) that the crosshair is "containing"

avatar image AndreasX12 · Oct 14, 2013 at 12:21 PM 0
Share

alt textThanks for the link, it's the closest I have ever gotten yet. But there's still one problem

It does work though, when the crosshair is in the center of the screen.

help2.png (202.2 kB)
avatar image AndreasX12 · Oct 14, 2013 at 12:33 PM 0
Share

Thank you SO SO SO much :D I got it working now! :D Thank you Again :D :D :D :D :D :D Feel free to convert to answer :-)

Show more comments

1 Reply

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

Answer by AlucardJay · Oct 14, 2013 at 12:07 PM

http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenPointToRay.html

This returns a ray going from camera through a screen point.

You can use this in a RayCast, check the last example in the RayCast Unity Scripting Reference : http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

Comment
Add comment · 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

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

18 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

Related Questions

Third Person Cross Hair Question 1 Answer

Help with Raycast C# 0 Answers

Using raycast and collider to increase int 1 Answer

NullReference when checking tag via Raycast. How to solve this? 1 Answer

Raycast exit point of collider 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