Skip to content

Instantly share code, notes, and snippets.

@elfefe
Last active April 29, 2023 17:32
Show Gist options
  • Select an option

  • Save elfefe/82eaba8a56d2cfcd8ac5dc6f412bc76a to your computer and use it in GitHub Desktop.

Select an option

Save elfefe/82eaba8a56d2cfcd8ac5dc6f412bc76a to your computer and use it in GitHub Desktop.
Wall detection in unity
// The distance where collision will be detected
public float collisionDistance = 3f; ///valeur debut Default
// Return available directions depending on player direction
private Vector3 AvailableDirection()
{
var direction = Vector3.zero;
if (ToucheMur(DirectionalRay(transform.forward)))
{
if (ToucheMur(DirectionalRay(transform.right)))
{
direction = -transform.right;
} else if (ToucheMur(DirectionalRay(-transform.right)))
{
direction = transform.right;
}
} else
{
direction = transform.forward;
}
return direction;
}
// Check if the player is touching the wall
private bool ToucheMur(Ray ray)
{
int layer_Mur = LayerMask.GetMask("Mur");
return Physics.Raycast(ray, collisionDistance, layer_Mur, QueryTriggerInteraction.UseGlobal);
}
// Create a ray from the player to the specified direction
private Ray DirectionalRay(Vector3 direction)
{
return new Ray(transform.position, direction);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment