Last active
August 29, 2015 14:01
-
-
Save DomDomHaas/79c8f44932f7063af851 to your computer and use it in GitHub Desktop.
Untiy raycasting in to ensure a character doesn't move inside a wall
This file contains 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
if (!isWallInFront (moveDirection)) { | |
this.rigidbody.MovePosition (this.rigidbody.position + this.transform.right * xAxis * xFacing); | |
} | |
private bool isWallInFront (Vector3 direction) | |
{ | |
if (UnityShortcuts.castRay (this.rigidbody.position, this.transform.right, rayCastLength, moveAlignLayer)) { | |
//Debug.Log ("got wall hit!"); | |
return true; | |
} | |
return false; | |
} | |
public static bool castRay (Vector3 pos, Vector3 direction, float rayLength, LayerMask moveAlignLayer) | |
{ | |
RaycastHit hit; | |
//Debug.DrawRay (pos, direction, Color.red, 1, true); | |
if (Physics.Raycast (pos, direction, out hit, rayLength, moveAlignLayer)) { | |
return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment