• 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 Voridian · Feb 23, 2014 at 12:58 PM · c#raycasttag

why doesn't this Raycast compare tag work? C#

hey again. Sorry to ask another question but can anybody figure out why this script does not work: using UnityEngine; using System.Collections;

 public class WeaponRaycast : MonoBehaviour {
 
     public GameObject Player;
     private RaycastHit hitCheck;
     float isRay = 0;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         Debug.Log(isRay);
         Vector3 playerTransform = Player.transform.forward;
 
         if(Input.GetButtonDown("Fire1")){
             if(Physics.Raycast(transform.position , playerTransform , out hitCheck , 20)){
                 if(hitCheck.collider.gameObject.CompareTag("Enemy")){
                     isRay = 1;
                 }
                 }
                 }
     }
 }

basically it should just say, if i click the mouse and something tagged as 'Enemy' is in front of me, make isRay = 1 but no matter what it equals 0. when i remove the CompareTag function, anywhere i click including the sky makes isRay = 1. This script it attached to my Player with the actual Player used as a GameObject.

Thanks for any help.

Comment
Add comment · Show 5
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 mattyman174 · Feb 23, 2014 at 01:35 PM 0
Share

Use Debug.DrawRay() to make sure that you are casting your Ray in the correct direction. It may not be targeting where you think it is and thus not hitting what you want it to.

http://docs.unity3d.com/Documentation/ScriptReference/Debug.DrawRay.html

avatar image sdgd · Feb 23, 2014 at 01:38 PM 0
Share

before if tag give this in please:

 Debug.Log(hitCheck.collider.gameObject.tag);
avatar image Voridian · Feb 23, 2014 at 02:47 PM 0
Share

@mattyman174: honestly i have no idea how to use a Debug.DrawRay. i barely understand how the raycast works, but i believe you are right in that the direction is wrong based on sdgd's suggestion (see below)

@sdgs: i did the Debug that you requested and it comes up with untagged. however i discovered that if i turn approximatly 90 degrees left the debug comes up as enemy so the raycast must be on the wrong axis.

so that leads to another question; how can i make the axis correct?

avatar image sdgd · Feb 23, 2014 at 03:16 PM 0
Share

I also saw some of your questions were solved you even said thanks but you never accepted answers, please accept answers that did help you and will help you.

avatar image Voridian · Feb 23, 2014 at 03:31 PM 0
Share

well the answers didn't really help completely, but at least i now know what is wrong, even if i cant fix it.

2 Replies

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

Answer by sdgd · Feb 23, 2014 at 03:09 PM

ok ray is 1 things.

origin + direction.

so from where it starts and where it goes.

to use Debug.Ray use:

  Debug.DrawRay(transform.position, transform.forward, Color.green);

ok this is odd, ...

  • you have GameObject Player let say facing to north.

  • you have this script attached to different object witch is facing south.

  • you are reading direction from Player while you need to read it from this Object.

try changing in to this:

 public GameObject Player;
 void Update () {
     Debug.Log(isRay);
     RaycastHit hit;
     if(Input.GetButtonDown("Fire1")){
         if(Physics.Raycast(Player.transform.position/*Origin*/, Player.transform.forward/*Direction*/, out hit/*referenced hit*/, 20f/*Distance - f so it won't be LayerMask*/)){
             if(hit.collider.gameObject.CompareTag("Enemy")){
                 isRay = 1;
             }
         }
     }
     // where is ray pointing - can be seen only inside Unity Editor - not Play mode
     Debug.DrawRay(transform.position, transform.forward, Color.green);
 }

you see how is it out there, well that's pretty much same as ref keyword if you ever come across it, ...

you give a pointer to hit so it can change it's value

  • and out means it ALWAYS changes it.

  • ref only means it gives pointer but doesn't need to change it.

I hope it's easier now.

I know rays were hard for me too in beginning.

Comment
Add comment · Show 3 · 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 Voridian · Feb 23, 2014 at 03:27 PM 0
Share

hey thanks for the answer. i adjusted the code to what you suggested and nothing was changed, its still raycasting from the wrong side and for some reason Debug.DrawRay does nothing that i can see (unless it dosnt get printed to console?). Thanks for the effort though. :)

avatar image sdgd · Feb 23, 2014 at 04:04 PM 0
Share

no Debug.DrawRay get's painted inside unity not to console.

for before the ray would be drawn from your script attached out.

since you cannot see anything I assume you need origin at Player.

that's why I edited the answer and this time.

The ray should be drawn out of Player.

avatar image Voridian · Feb 23, 2014 at 04:14 PM 0
Share

cant see it anywhere...

EDIT: OHHH! i see it, i had maximise on play selected... yeah the origin is munted as hell. its a little behind the player and facing the wrong way

avatar image
0

Answer by PsychoPsam · Oct 27, 2016 at 12:41 PM

CompareTag doesn't work in builds in version 5.3.4

Use if (hit.collider.gameObject.tag == "Enemy")

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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 stops when false, even in update() 1 Answer

C# Raycast 2D GameObject follow Mouse 1 Answer

Finding Distance between Angles and Points 2 Answers

Changing a Variable within the same script 0 Answers

How can i change a variable on the object hit by a raycast C# 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