I use raycast to hit enemy, then why does enemy get damaged when i shoot over them??

Hello! I was just making a little FPS game and look what i found! If i try to shoot JUST over the head of my enemy, they still receive damage! Please help.

P.S. photo and script attached ::

30700-untitled.png

if (Input.GetButtonUp ("Fire1")) 
		{
			if (Physics.Raycast(pos1, dir, out shot, 20f)) 
			{	selectedTarget = shot.collider.gameObject;
				enemyhp = selectedTarget.GetComponent<enemyHp> ();
				enemyhp.curHealth = enemyhp.curHealth - 10;
				if (enemyhp.curHealth <= 0) {
					Destroy(selectedTarget);
					selectedTarget = null;
				}

			}
		}

THANKS!

  1. check if the collider for the enemy fits properly as @BoredMormon says.

  2. Draw a debug ray from your gun to check if the ray is pointing in the right direction, The code below should work in your project.

Debug.DrawRay(pos1,dir*20,Color.red);

That will draw a ray in the editor window, if you are in the game window you have to enable gadgets to see the ray.

Check out how big the collider on the enemy is. Common capsule colliders don’t always fit perfectly.

Also check that there is no trigger collider attached. The default in Unity is for raycasts to hit triggers, but you can turn this behaviour off.