Skip to content

Instantly share code, notes, and snippets.

@DomDomHaas
Last active August 29, 2015 14:01
Show Gist options
  • Save DomDomHaas/79c8f44932f7063af851 to your computer and use it in GitHub Desktop.
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
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