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 /
This question was closed Mar 29, 2015 at 02:59 PM by Digital-Phantom for the following reason:

Problem is outdated

avatar image
0
Question by Digital-Phantom · Mar 28, 2015 at 05:36 PM · raycastplayerobjectstagraycasthit

Make Raycast ignore anything that "Isn't" my player(Solved)

kind of a question in reverse but here goes.

I can get my enemy to track and fire at my player using a raycast and Tag combination.

 if(Physics.Raycast(shootingRay, out hit, attackDistance))
         {
             if(hit.collider.tag == "Player")
             {
                 InvaderFireWeapon();
             }
         }
 

But.. if I have an object between my enemy and the player it still fires as the raycast just goes through the obstruction?

Or to put it another way how do I make it so only a direct hit by the raycast will result in an attack

???

Comment
Add comment · Show 1
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 ArkaneX · Mar 28, 2015 at 05:53 PM 0
Share

It looks like the obstacle doesn't have any collider attached. That's why raycast passes through it.

1 Reply

  • Sort: 
avatar image
0

Answer by HappyLDE · Mar 28, 2015 at 06:04 PM

You need to yous the layermask:

 public LayerMask layerMasks;
 
 if (Physics.Raycast(shootingRay, out hit, attackDistance, layerMasks))

Comment
Add comment · Show 4 · 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 ArkaneX · Mar 28, 2015 at 06:23 PM 1
Share

This won't help, if there's no collider on the obstacle...

avatar image Digital-Phantom · Mar 28, 2015 at 06:40 PM 0
Share

The objects that are acting as obstacles do have colliders.

avatar image Digital-Phantom · Mar 29, 2015 at 02:50 PM 1
Share

I tried adding a debug.log to the script and didn't get anything. So I added a debug.Drawray and started to check every object in the scene up close, rotation, size, transform position and DOH....

The raycast was missing the collider by 0.04 units. In perspective view everything was great, wasn't until I started using ortho view I noticed it.

Definitely one of those face meet desk moments....lol

Anyway, thanks guys

:/

avatar image I_Am_Gamer · Mar 29, 2015 at 02:53 PM 0
Share

gotta give a thumbs up just for the "face meets desk" comment, made me chuckle.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Raycast tag/ Null reference exception? 1 Answer

Switch GameObjects Tags with javascript 1 Answer

Crash in : UnityEngine.RaycastHit:INTERNAL_CALL_CalculateRaycastTexCoord 1 Answer

Raycast Based on GameObject Tag 0 Answers

Why is this Coroutine not working ? 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