A special raycast ?

Greetings,

I am facing some troubles with Raycasting, and I wonder if there is a way to ignore some colliders ?

Using layers is a solution, it could solve my problem. But I would like to avoid using it (would bring some mess, and ask a complex code). Is there a way to ignore precise colliders (two, to be precise) when raycasting ?

i would just set those 2 conflicting colliders to the ignore raycast layer, honestly i dont see why that would complicate your code

Because one of the two colliders is not always in the layer ignored :wink:

I have the player, a sphere collider, and multiples cubes. The sphere goes from cubes to cubes. The raycast is used to detect a cube in a direction. The cube on which the player is, must be ignored.

But when the player leave it, it must be detected again :slight_smile:

I guess I have to add two functions / messages, telling the current / previous cube to be or not to be in the Player layer.

Hello,

When you hit the collider, you get the transform behind it. Just use its name :

		Physics.Raycast (myself.position, rayfwd, hitfwd, distanceForRayCast)


		if (hitfwd.transform)
	   	{
			if(hitfwd.transform.name == "voiture") 
                                ......
           }

This doesn’t work quite perfectly, as a raycast stops once it hits a collider (rather than actually ignoring it).

One option is to use RaycastAll, then loop through the hits and check to see if it’s what you’re looking for. However, I’d assume this is fairly slow since it has to raycast through every object (I could be wrong, not exactly a 3d graphics programmer here).

Better way to do it would be something like this…

	var layerMask = 1 << layer;
	layerMask = ~layerMask;
	
	gameObject.layer = 10;
	
	var hit : RaycastHit;
	
	if (Physics.Linecast(transform.position, target.position, hit, layerMask))
	{
//Linecast that ignores the immediate gameObject
	}
	
	gameObject.layer = startingLayer;

startingLayer is a private var that is assigned in the Start() function. It is the layer that the gameObject starts on when it’s initialized.

This basically just creates a layermask that ignores layer 8 (random number), but detects all others. It adds this gameObject to the layer, performs the linecast and then switches the layer back. Works like a charm!

Yeah, I did something like that finally (put the object to ignore to the player layer, and put it back to default after moving out of him) :wink: It took less code than I thought, but it required a good amount of trickery.