Raycasting Problems

Hi there!

I am having problems with raycasting.

I am raycasting shots out of players weapon and visualising them with drawray.

However, sometimes raycasts hit Character Controller Collider itself, even thought drawray indicates no such thing occurring. (Blood spatter on ground is indication of damage receive. That or ragdoll replacing my character…)

Therefore, I suspect I am doind something very very wrong.

Here’s a snippet of that raycasting magic (magically crappy code):

	RaycastHit hit;
        		if (Physics.Raycast(PSTLpoint.transform.position, new Vector3(cursor.transform.position.x,PSTLpoint.transform.position.y,cursor.transform.position.z)-PSTLpoint.transform.position+accDeviation, out hit, 400.0F))
				{
					WInfo.inMagSMG--;
					//Debug.Log ("Ammo left: " + WInfo.inMagPSTL);
	            	float distanceToGround = hit.distance;
					Debug.DrawLine(PSTLpoint.transform.position, hit.point, Color.red, 100, false);
					if(hit.transform.tag=="Player")
					{
							damageAmount=Random.Range(WInfo.damMinSMG,WInfo.damMaxSMG);
						hit.transform.gameObject.GetComponent<PhotonView>().RPC("ApplyDamage", PhotonTargets.All, damageAmount, myID.viewID, Mathf.RoundToInt(hit.normal.x), Mathf.RoundToInt(hit.normal.y), Mathf.RoundToInt(hit.normal.z));
						
						
						
					}

Add a layermask to your Raycast to discount the player from the cast.

As meat5000 suggested, adding layermasks fixed the problem. The actual problem itself was microsized box collider at pistol object that was practically invisible.

Thanks!