#Raycast always returns null

1 messages · Page 1 of 1 (latest)

meager haven
#

Hi, i'm working on an enemy AI for my game that uses a raycast to determine if it can fire upon you or if there's an obstacle in the way, but after i was done fixing another bug the raycast started to not give me any info (null) as if it didn't hit anything, eventhough it did

code:

if(Physics.Raycast(new Vector3(EnemyDirectionMeasurement.transform.position.x, EnemyDirectionMeasurement.transform.position.y, EnemyDirectionMeasurement.transform.position.z + 1), Vector3.forward, out hit, Mathf.Infinity))
        {
            hitObject = GameObject.Find(hit.collider.gameObject.name).GetComponent<UnitBehaviour>();
            if(hit.collider == preferedEnemy)
            {
                isEnemyInFireRange = true;
            }
            else
            {
                isEnemyInFireRange = false;
            }

            //Set isEnemyInFireRange to false if the raycast doesn't hit anything
            if(hit.collider == null)
            {
                isEnemyInFireRange = false;
            }
        }
rugged reef
#

What variable is null, exactly?

meager haven
#

hit.collider

rugged reef
#

That should throw a NullReferenceException, then, right?

meager haven
#

no

rugged reef
#

Well, you're calling hit.collider.gameObject, so if it were null, you'd get an exception.

#

Are you sure it's null?

meager haven
#

I put a Debug.Log displaying hit.collider at the end and it throws a null

rugged reef
#

Did you put the Debug.Log inside the if-statement?

meager haven
#

nope

rugged reef
#

Great, so it didn't actually hit anything.

#

as if it didn't hit anything, eventhough it did

#

So it really doesn't hit.

#

Then the code doesn't really say that much, sadly.

#

I'll throw out some ideas for what could be wrong.

meager haven
rugged reef
#

I don't think the code is the important part either.

#

I think it's the setup.

#

Like colliders.

meager haven
#

basicly there's an empty gameobject that alsways turns towards the target and shoots the raycast

#

that's how it's set up

rugged reef
#

Okay, I have an idea.

meager haven
#

i even did a Debug.DrawRay with the same parameters as the raycast and it goes through walls and stuff so it's hitting but the raycast doesn't realize that

rugged reef
#

Hm, can I see your ray?

#

Well, the code for drawing it.

#

I was going to suggest that. :P

meager haven
#

sure give me a sec

#
Debug.DrawLine(new Vector3(EnemyDirectionMeasurement.transform.position.x, EnemyDirectionMeasurement.transform.position.y, EnemyDirectionMeasurement.transform.position.z + 1), GameObject.Find(preferedEnemy.GetComponent<Collider>().gameObject.name).transform.position, Color.red, 0, false);
rugged reef
#

Hmm.

#

That doesn't look like the same ray.

#

The raycast uses Vector3.forward as the direction.

#

I'm just worried that's the issue.

#

I'd extract the origin and direction as variables, possibly into a Ray.

#

And use that Ray for both the Raycast and the DrawLine/DrawRay.

#

Just to make sure.

meager haven
#

ok give me a minute

white shadow
#

also, the origin can be determined more easily:
EnemyDirectionMeasurement.transform.position + Vector3.forward
will give you the same result as
new Vector3(EnemyDirectionMeasurement.transform.position.x, EnemyDirectionMeasurement.transform.position.y, EnemyDirectionMeasurement.transform.position.z + 1),
while also being more concise.

rugged reef
#

Yeah, I think simplifying the code can make it easier to spot the error.

meager haven
rugged reef
#

No, the code is literally equivalent.

#

There is no difference between your code and his; it's just a simplification.

meager haven
rugged reef
meager haven
rugged reef
#

If your code does that, then yes, this wouldn't change it.

meager haven
white shadow
rugged reef
#

Okay, so how about you use a LayerMask instead?

white shadow
#

Though I am assuming that you will want to be using local directions for your ray, rather than global ones in the first place.

rugged reef
#

Yeah, a simple one would be Physics.Raycast(transform.position, transform.forward, out var hit).

#

Then I would add a LayerMask to make sure it doesn't hit itself.

rugged reef
#

So, when you're shooting a Raycast, you can give it a LayerMask as a parameter.

#

This will tell it which layers to ignore.

#

You can assign layers to GameObjects.

#

Like Player or Enemy.

#

And you specify that your raycast just ignores the Enemy layer or only hits Player.

#

It's probably not that simple, but layers can be useful here.

#

But I'm not convinced your raycast would hit the enemy anyway...

#

If you have a SphereCollider, and you shoot a raycast from inside it, it won't hit the sphere itself.

meager haven
#

But thanks for the help guys