- Home /
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.
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
before if tag give this in please:
Debug.Log(hitCheck.collider.gameObject.tag);
@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?
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.
well the answers didn't really help completely, but at least i now know what is wrong, even if i cant fix it.
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.
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. :)
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.
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
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")
Your answer
![](https://koobas.hobune.stream/wayback/20220613134441im_/https://answers.unity.com/themes/thub/images/avi.jpg)