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?
}