Created
January 20, 2018 12:07
-
-
Save IJEMIN/d62b2d85816bedf7c80b810cd57e5f55 to your computer and use it in GitHub Desktop.
How to Raycast by Angle in Unity
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
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class RayByAngle : MonoBehaviour { | |
public float angle; | |
public float distance = 1f; | |
// Update is called once per frame | |
void Update () { | |
ShootRay(); | |
} | |
void ShootRay() | |
{ | |
var direction = Quaternion.AngleAxis(angle, transform.right) * transform.forward; | |
Ray ray = new Ray(transform.position,direction); | |
Debug.DrawLine(ray.origin,ray.origin + ray.direction * distance,Color.red); | |
if(Physics.Raycast(ray,distance)) | |
{ | |
Debug.Log("Something is there"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this, I needed to do this to get my characters to not be able to walk up slopes greater than this angle and it worked perfectly. It also solved the problem of my characters sometimes getting stuck on the edges of what the old code this replaced was hitting.