Making a raycast ignore specific collider

Hello,

I have 100 turrets. each turret have a collider and sends a raycast to the player.
If i stand behind the turret, the raycast hits the turret’s collider first.

I understand about making it ignore a layer, but i don’t want it to ignore all colliders on the other 99 turrets. just it’s own. and obviously i don’t create a layer for each turret.

so how do i get it done?

Raycasts won’t trigger on a collider if they start inside it, so if you’re using one collider for your turret you can simply start the raycast from inside it. If you really do want to start the raycast outside the turret though, you don’t really need to use empty gameobjects, just a bit of math.

using UnityEngine;
using System.Collections;

public class TurretRaycast : MonoBehaviour
{
	public Transform player;
	Vector3 raycastOrigin;
	Vector3 raycastDirection;
	float raycastStartRange = 2f; //How far from the center of the turret to start raycast
	RaycastHit hitInfo;

	void Update()
	{
		raycastDirection = (player.position - transform.position).normalized;
		raycastOrigin = transform.position + (raycastDirection * raycastStartRange);
		if(Physics.Raycast(raycastOrigin, raycastDirection, out hitInfo))
		{
			//Check hit
		}
	}
}

Note that if the player stands directly against the turret using that code, the raycast origin could be within the player’s collider, so if possible have the origin inside the turret collider.