# Something like OnTriggerExit for Raycast?

Hi! I’m working in a 2d Puzzle game and I’m looking for something like OnTriggerExit for the Raycast. I’m making a script: I have a few cubes making the rol of walls, and a single player cube. The player cube have 4 diagonal raycast for each edge. When the player pass side of a cube wall any of the 4 raycast will detect that cube wall and will set isTrigger = true, to that cube wall. This keeps the player movement naturally, because it wont stop at the wall edge when hits. What i’m looking for is: When the raycast out of the cube wall set the cube wall isTrigger return to false.

(The Wall cubes have already “Wall” tag, and the Script already works for the ‘isTrigger = true’ when hits (All the Walls and cubes are 3D)

I need help, because the ‘else’ don’t works at least how I want

Sorry, I hope you haven’t been confused

Thanks!

Script:

``````RaycastHit hit;
Ray Sup_izq = new Ray(Ray_Sup_izq.position, new Vector3(-1f, 0f, 1f));
Ray Sup_der = new Ray(Ray_Sup_der.position, new Vector3(1f, 0f, 1f));
Ray Inf_izq = new Ray(Ray_Inf_izq.position, new Vector3(-1f, 0f, -1f));
Ray Inf_der = new Ray(Ray_Inf_der.position, new Vector3(1f, 0f, -1f));

Debug.DrawRay(Ray_Sup_izq.position, new Vector3(-1f, 0f, 1f), Color.magenta, 1f);
Debug.DrawRay(Ray_Sup_der.position, new Vector3(1f, 0f, 1f), Color.blue, 1f);
Debug.DrawRay(Ray_Inf_izq.position, new Vector3(-1f, 0f, -1f), Color.cyan, 1f);
Debug.DrawRay(Ray_Inf_der.position, new Vector3(1f, 0f, -1f), Color.green, 1f);

if (Physics.Raycast(Sup_der, out hit, 3f) || Physics.Raycast(Sup_izq, out hit, 3f) || Physics.Raycast(Inf_izq, out hit, 3f) || Physics.Raycast(Inf_der, out hit, 3f))
{
if (hit.collider.gameObject.tag == "Wall")
{
hit.collider.isTrigger = true;
//si esta aqui es porque el raycast detecto una pared
print("choque con " + hit.collider.name);
}
}
``````

You should attach raycast to walls and detect player is arround or not, its better way.

otherwise you have to keep track of all walls, so in the case of no detection you can set there trigger off

if(Physics.Raycast(ray, out hit,deplovementheight))
{

``````   if(hit.collider.tag == "Player"){
this.GetComponent<Collider>().isTrigger = true;
}
else
{
//if it collides but its not Player
this.GetComponent<Collider>().isTrigger = fasle;
}
``````

}

//if it collides with nothing

else
{

`````` this.GetComponent<Collider>().isTrigger = fasle;
``````

}