Raycast Ignore Own Collider Without Layers

Is there a way to do this?

I’ve got an object I wish to duplicate many times. I don’t want to use layers because I want the raycast to hit the duplicate’s SphereCollider, just not each one’s own collider.

Tried to move the origin of the raycast to the edge of collider, but so far I have been unsuccessful. Do I need to RaycastAll? Can someone point me in the right direction?

Here’s a simple example C# script. It continually hits itself.

using UnityEngine;
using System.Collections;

public class SimpleAI : MonoBehaviour {
	public float speed = 20;
	public float rotateSpeed = 10;
	public RaycastHit hit;

	void Start () {
		//
	}
	
	// Update is called once per frame
	void Update () {
		//SphereCollider mCollider = (SphereCollider)GetComponent(typeof(SphereCollider));
                //Vector3 adjOrigin = transform.position + Vector3.forward * mCollider.radius;
		if(!Physics.Raycast(transform.position, transform.forward, out hit, 10)){
			transform.Translate(Vector3.forward * speed * Time.smoothDeltaTime);
		} else {
			if(hit.collider.gameObject.tag == "npc"){
				Debug.Log("I hit myself, or someone in my group.");
			} else if(hit.collider.gameObject.tag == "player"){
				Debug.Log("I hit the player");
				transform.Rotate(Vector3.up, 90 * rotateSpeed * Time.smoothDeltaTime);
			} else if(hit.collider.gameObject.tag == "ground"){
				Debug.Log("I hit the ground.");
			} else {
				Debug.Log("Something is seriously messed up.");
			}
		}
	}
}

Just try RaycastAll. You may have another member variable that is initialized on Start or Awake.

private SphereCollider ownCollider;

void Start () {
	ownCollider = GetComponentInChildren <SphereCollider> ();
}

Now you simply have to ignore ‘ownCollider’ when checking the array you are getting with ‘RaycastAll’.
It’s straight forward, as long as you don’t get performance issues.

1 Like

Hi guys,

I’m having a similar issue to the OP, in that I’m trying to prevent a raycast (fired from a hand-held weapon) from detecting the collider of the parent object (Player) that it is attached to.

I’ve gotten as far as creating a private Collider variable that is set to the root object’s collider (in this case, the Player), then only applying damage if the raycast doesn’t hit the Player’s collider. Code snippet below:

public class BurstRifle : MonoBehaviour 
{
	private Collider ownCollider;

	void Start ()
	{
		// Set the weapon owner's collider as ownCollider.
		ownCollider = transform.root.gameObject.GetComponent<Collider>();
	}

	void  FireOneShot ()
	{
		Vector3 direction= transform.TransformDirection(Vector3.forward);
		RaycastHit hit = new RaycastHit();
		
		// Did we hit anything?
		if (Physics.Raycast (transform.position, direction, out hit, range)) 
		{
			// Test to ensure we don't damage ourselves.
			if (!ownCollider)
				// Send a damage message to the hit object			
				hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
		}
	}
}

Using the above code, the Player’s gunshots no longer hurts himself… but it now does no damage to enemies either. I’m not 100% familiar with raycasting code, but I’m guessing that I need to use RaycastAll to get the gunshots to register on enemy colliders if I’m ignoring the Player’s own collider.

After many hours(!) trying to make heads from tails of the RaycastAll API document, I really have no idea how to convert my existing RaycastHit behaviour into RaycastAll, especially as RaycastAll looks like it also uses arrays (another C# coding syntax that I haven’t fully grasped yet).

Any conversion help, or even a better explanation as to why my code is preventing gunshots from damaging enemy colliders, would be much appreciated!

Right now it looks like your hit checking code is only checking to see if ownCollider exists at all, not if that was what was hit.

I may be wrong of course (fairly new at this) but that’s jumping out at me.

Maybe try replacing !ownCollider with (hit.collider != ownCollider) or something of the sort.

edit: this is to dlcl

@PunsAndAmmo

You, Sir, are a gentleman and a saint. You’ve nailed the problem right on the head. Using if(hit.collider != ownCollider) fixed the problem. Thanks!

Well, all I have is a hammer…

Also, glad I could help!

 ownCollider = transform.root.gameObject.GetComponent<Collider>();

I get errors because I am writing in java. :frowning:

As your issue isn’t the same one as in this thread, you should start a new one.

A very good solution is temporarily disable your collider, make the raycast hit and then activate it again after the raycast

Or you can simply disable it in the Player Setting >> Physics 2D >> Queries Start In Colliders >> Uncheck

1 Like