Last active
April 29, 2023 17:32
-
-
Save elfefe/82eaba8a56d2cfcd8ac5dc6f412bc76a to your computer and use it in GitHub Desktop.
Wall detection in unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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