Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Arustyred · Dec 25, 2017 at 06:06 PM · raycastcollidercollision detectionraycastingissue

Raycast not hitting collider

Hello, I am trying to add spotting similar to battlefield 3 and 4. All the players have a box collider around them that is on the layer "tagging." I have the player send out a raycast whenever they press 'E' and a layermask on the ray so that it only collides with the tagging layer. It works fine when I am relatively close to the player. However, when I get more than 10-50 units away from him, it doesnt detect the collision at all. Also, the raycast maxDistance is over 600 so that shouldnt be the problem.

Code:

 void RpcSendRay(){
     RaycastHit shot;
     Debug.Log (transform.name + " sent ray"); //this is always logged
     if(Physics.Raycast (raycastItem.position, raycastItem.forward, out shot, 600f, mask.value)){
         Debug.Log (shot.transform.name + " ray hit"); //I never get to this point unless really close
         Debug.Log (shot.distance);
         PlayerTagging pt = shot.transform.GetComponent<PlayerTagging> ();
         if (pt != null) {  //this if statement just makes sure they have the script and then tags them
             if (pt.GetComponent<Player> ().isLocalPlayer) {
                 return;
             }
             Debug.Log (shot.transform.name + " getting tagged");
             pt.tagMe ();
         }
     }
 }
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
0
Best Answer

Answer by Arustyred · Dec 26, 2017 at 01:21 AM

Alrighty, I got it. I'm not sure why but it is because I send out the ray in the clientRpc. I just moved it so that it is called locally and then it calls a command/rpc if it actually hits someone.

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
avatar image
0

Answer by Bieere · Dec 25, 2017 at 07:09 PM

It could be that your forward vector is offset slightly, and could explain why it works at a close distance, and not at a further one, where the ray is actually going in a direction around the gameObject you're attempting to hit.

To see if this is in fact the issue, normalize your forward vector, and see if the issue is still there.

 if(Physics.Raycast (raycastItem.position, raycastItem.forward.normalized, out shot, 600f, mask.value))
 {
 [...]
 }
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 Bieere · Dec 25, 2017 at 07:12 PM 0
Share

You should also do a Debug.Ray() to see where exactly the ray is going

avatar image Arustyred · Dec 26, 2017 at 12:55 AM 0
Share

Thanks for responding. I have that in my Update function now and it is pointing straight. Any other ideas?

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

112 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 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 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 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 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 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

How can I let the ray to the edge of the capsule? 1 Answer

Raycast exit point of collider 0 Answers

Restrict Rigidbody movement in the Z axis 1 Answer

Fixing Editor Mouse Offset Due To Local Raycast? 1 Answer

SInce when?!?!? (Raycast issue) 2 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