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 marf · Jan 02, 2012 at 01:11 PM · raycastscreenraycasthitscreenpointtoraymiddle

Raycast to middle screen doesn't work

Hi, I have this code:

 int x = Screen.width / 2;
 int y = Screen.height / 2;

 Ray ray = Camera.main.ScreenPointToRay(new Vector3(x, y));
 RaycastHit hit = new RaycastHit();

 Physics.Raycast(ray, out hit);

     if (hit.collider.tag == "Player")
     {
        
          SomeFunction();

     }

The problem is that the if statement is never true, I have a crosshair (GUITexture) drawed in the middle of the screen, when I focus it on a player and I shoot it's like that I don't shoot to the player.

Any suggestions?

Marco

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 aldonaletto · Jan 02, 2012 at 01:50 PM

You should use an if to avoid null reference errors, and there's no need for the new RaycastHit instruction, but the code seems ok.
It's easier to use ViewportPointToRay to create a ray from the middle of the screen - just pass Vector3(0.5f, 0.5f, 0). Try something like the code below - it includes a debug instruction to show the hit object's name, if any:

Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); RaycastHit hit;

if (Physics.Raycast(ray, out hit)){ print("Hit "+hit.collider.name); if (hit.collider.tag == "Player"){ SomeFunction(); } }

Comment
Add comment · Show 6 · 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 marf · Jan 02, 2012 at 01:57 PM 0
Share

It says every time Hit Terrain, that's like that it don't recognize the player, but it has the tag Player attached, there is a way to draw the ray so I can view it?

avatar image Statement · Jan 02, 2012 at 02:20 PM 1
Share

@marf, Debug.DrawLine

avatar image marf · Jan 02, 2012 at 02:28 PM 0
Share

@Statement I think that this only work in 2D games, I should you a Line Render, but I don't know how.

avatar image aldonaletto · Jan 02, 2012 at 04:09 PM 1
Share

Use Debug.DrawRay right after the print:

 print("Hit "+hit.collider.name);
 Debug.DrawRay(ray.position, 100 * ray.direction, Color.red);
 ...

The ray starts at the camera position, thus in the Game view you only see a $$anonymous$$uscule red dot at the center of the screen - and only if you click the Gizmos button on the top of the Game view. An alternative is to select Windows/Layout/2 by 3, what lets the Game and Scene views side by side, thus you can see the ray in the Scene view.

avatar image Bunny83 · Jan 02, 2012 at 04:52 PM 1
Share

@marf: Just fyi: Debug.DrawLine /.DrawRay only works in the editor for debug purposes. If you build your game those lines aren't visible. The type of game is irrelevant since Unity is always a 3D-engine, even when you just use 2 dimensions.

$$anonymous$$ake sure your player have any kind of collider (BoxCollider, CapsuleCollider, SphereCollider or a CharacterController which is also a collider!) attached. If the collider is attached to a child object make sure the child has also the "Player" tag.

Show more comments

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

7 People are following this question.

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

Related Questions

Raycasting Accurately with gun 2 Answers

How to make constant click position ? 0 Answers

Not getting right value; ScreenPointToRay 0 Answers

Raycast hits everything on the screen 2 Answers

raycast screenpointtoray isnt working for some reason 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