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 iruinedyourday · Apr 08, 2017 at 10:58 PM · c#raycastraytaggingcomparetag

Why wont my raycast compareTag properly?

I have a ray that shoots out, if it hits anything it spits out a public string of the gameobject it hits and the distance to it.

But to refine it, I created a Tag named "Target", tag my game object I want the ray to notice, then I made an if statement to compareTag so it will only notice if it hits a game object tagged as a "Target" (Down the road I want this ray ignored completely UNLESS it hits a target, then I want a special bullet to come out, or special effect to play.)

I think my code looks right, but it still spits out any game object to the string, regardless if the gameobject is tagged as a Target or not.

I also notice that it only spits out the else string, "null" when the ray hits NO game object at all, but only when that happens, if it hits an untagged(as "Target") game object it still prints the name. As if the tag is not a concern at all for it, only whether its hitting a game object or not.

Maybe I am doing this all wrong, but here is my if/else's for if the ray hits something and if it matches the tag, spit out the gameobject string into the public var and the distance to said gameobject (C#):

 void Update()
 {
             if (Physics.Raycast(transform.position, (forward), out rayHit))
             {
                 if (rayHit.collider.CompareTag("Target"))
                 theDistance = rayHit.distance;
                 targetObject = (" " + rayHit.collider.gameObject.name);
             }
             else
                 targetObject = ("null");
             //hmm why is this only spitting out null if its not aiming at anything?          
 }
 
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
1
Best Answer

Answer by k234234w · Apr 09, 2017 at 05:34 AM

Physics.Raycast returns bool True if the ray intersects with a Collider, otherwise false. When hitting no object it would go to the else then and print out your null. In your condition comparing target, the if only braces your distance assignment, so object target or not is running that name print out line if it passes the Physics.Raycast condition. Brace both lines. Additionally I would use a layermask, if that makes sense for your project. For example if you have a target type you can assign its layer to target, that way you can use that layermask in the Physics.Raycast, and your Raycast will only consider targets. Upon further looking I think you want this as well.

 if (rayHit.collider.gameObject.CompareTag("Target"))
 {
 // Whatever
 }

Comment
Add comment · Show 1 · 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 iruinedyourday · Apr 09, 2017 at 05:44 AM 0
Share

I was missing the brackets, now everything works as expected! Thank you!

avatar image
0

Answer by Delforce · Apr 09, 2017 at 05:36 AM

Hi mate, I see 2 things with your code;

1) The question in your comment is basically asking why you only get a null when the ray cast hits nothing which is because the ray cast is either hitting something or not. Your else may be in the wrong place.

2) Also the line where your If is located to Check the tag of the object collided by the ray has no brackets to support only showing when the tag Target is hit. Put some brackets around it like so.

              if (rayHit.collider.CompareTag("Target"))
              {
                  theDistance = rayHit.distance;
                  targetObject = (" " + rayHit.collider.gameObject.name);
              }

That way you enclose the distance and object name to only occur when the tag is target types.

Based on my understanding of your question.

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 iruinedyourday · Apr 09, 2017 at 05:43 AM 0
Share

This worked but I just accepted the answer above! It was the brackets! I am new to program$$anonymous$$g :) Now it prints out my null statement properly thank you!

avatar image Delforce iruinedyourday · Apr 09, 2017 at 08:36 AM 0
Share

Glad to have helped mate.

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

8 People are following this question.

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

Related Questions

C# mouse Raycast question 3 Answers

What's wrong with my RaycastHit2D? 1 Answer

Rays and tags help? 1 Answer

ray transform.position goes wrong 1 Answer

This code only works on 3D Colliders and I want it to work on 2D. What do I need to change? 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