RayCast hits Player

Hi all,
I have this player raycast shooting script which works fine, but sometimes when I am shooting the player will take damage and I am unsure why.
Thanks :slight_smile:

using UnityEngine;
using System.Collections;

public class PlayerShooting : MonoBehaviour {
	
	public float fireRate = 0.5f;
	float cooldown = 0;
	public float damage = 25f;
	public Texture2D crosshairImage;
	
	float spreadFactor;
	
	public bool canFire = true;
	
	public GameObject Hud;
	FXManager fxManager;
	
	void Start()
	{
		fxManager = GameObject.FindObjectOfType<FXManager> ();
	}
	
	void Update () {
		cooldown -= Time.deltaTime;
		
		if(Input.GetButton("Fire1") && (canFire == true)) {
			// Player wants to shoot...so. Shoot.
			if(Input.GetMouseButton(1))
			{
				animation.Play("ZoomInFire", PlayMode.StopAll);
				spreadFactor = 0.01f;
			}
			else if(!Input.GetMouseButton(1))
			{
				animation.Play("AK5CHip", PlayMode.StopAll);
				spreadFactor = 0.03f;
			}
			Fire ();
			
		}
		
	}
	
	void Fire() {
		if(cooldown > 0) {
			return;
		}
		
		//Spread firing rate
		Vector3 direction = Camera.main.transform.forward;
		direction.x += Random.Range(-spreadFactor, spreadFactor);
		direction.y += Random.Range(-spreadFactor, spreadFactor);
		direction.z += Random.Range(-spreadFactor, spreadFactor);
		
		
		Ray ray = new Ray(Camera.main.transform.position,direction );
		Transform hitTransform;
		Vector3   hitPoint;
		
		hitTransform = FindClosestHitObject(ray, out hitPoint);
		
		
		if (hitTransform != null) {
			Hud.GetComponent<HUD> ().ReduceAmmo ();
			
			Health h = hitTransform.GetComponent<Health> ();
			
			while (h == null && hitTransform.parent) {
				hitTransform = hitTransform.parent;
				h = hitTransform.GetComponent<Health> ();
			}

			
			if (h != null) {
				PhotonView pv = h.GetComponent<PhotonView> ();
				if (pv == null) {
					Debug.LogError ("Freak out!");
				} else {
					h.GetComponent<PhotonView> ().RPC ("TakeDamage", PhotonTargets.AllBuffered, damage);
				}
				
			}
			
			if (fxManager != null) {
				hitPoint = Camera.main.transform.position + (Camera.main.transform.forward*100f); 
				fxManager.GetComponent<PhotonView> ().RPC ("SniperBulletFx", PhotonTargets.All, Camera.main.transform.position, hitPoint);
			}
		}
		else {
			
		}
		
		cooldown = fireRate;
	}
	
	Transform FindClosestHitObject(Ray ray, out Vector3 hitPoint) {
		
		RaycastHit[] hits = Physics.RaycastAll(ray);
		
		Transform closestHit = null;
		float distance = 0;
		hitPoint = Vector3.zero;
		
		
		
		foreach(RaycastHit hit in hits) {
			if(hit.transform != this.transform && ( closestHit==null || hit.distance < distance ) ) {
				closestHit = hit.transform;
				distance = hit.distance;
				hitPoint = hit.point;
			}
		}
		
		// closestHit is now either still null (i.e. we hit nothing) OR it contains the closest thing that is a valid thing to hit
		
		return closestHit;
		
	}
	
	void OnGUI()
	{
		float xMin = (Screen.width / 2) - (crosshairImage.width / 2);
		float yMin = (Screen.height / 2) - (crosshairImage.height / 2);
		if (!Input.GetMouseButton (1)) 
		{
			GUI.DrawTexture (new Rect (xMin, yMin, crosshairImage.width, crosshairImage.height), crosshairImage);
		}
	}
}

Iā€™m new to this, so I apologize if I am wrong. But I would ray cast to a layer, or layers, whichever may suit you, and make sure that the player is not on one of those layers. Because I believe that if you ray cast to a specified layer(s), only objects on those layer or layers can be hit.

Here is what I did:

int tt = 1 << 15;   //raycasting on #15 which is the layer you want to raycast on

Ray ray = new Ray();  // my ray 

ray = (Camera.main.transform.position,direction );  //look at what my mouse in looking at

if (Physics.Raycast (ray, out hit, 1000, tt)
{
//do something
}

Hope that helps

i dont know if this works on c# too but in javascript you can just do this:

var mask : LayerMask;

function Update ()
{
    var hit : RaycastHit;
    if(Physics.Raycast(ray, hit, 10, mask))
    {
        //do stuff
    }
}