Raycast only seems to hit the collider

I have a Raycast that goes from a Unit towards an enemy with a cone effect. This is to simulate machine gun fire. But for some reason when using the following code my Rays will only hit the enemy and not around it. I have a feeling it’s something to do with the out shootHit in the Physics.Raycast if statement.

I just can’t find the cause of this issue. Any help would be greatly appreciated.

Code below:

		shootDirection = rotateTransform.forward + (rotateTransform.right * Random.Range(-shootAccuracy, shootAccuracy));	
		RaycastHit shootHit;	
		if (Physics.Raycast(transform.position, shootDirection, out shootHit, shootRange))
		{
			Debug.DrawRay(transform.position, shootDirection * shootRange);
			if (shootHit.transform.tag == "Enemy")
			{
				Debug.Log(D.Log(this, shootHit.transform.name + " HIT with " + activeAbility + " - Health is "));
			}
		}

1 Answer

1

A raycast does only hit colliders so you’re right there.

If an object does not have a collider, it cannot be hit with a ray cast. Moreover, you can tell your ray casts to only cast on a particular mask layer (which you can see from the Physics.Raycast function overloads).

Here’s some documentation to look over:

That makes perfect sense. I was expecting the Rays to be cast around an Enemy even though there was nothing for it to collide with, little bit of confusion from me there. Thanks dude.