Skip to content

Instantly share code, notes, and snippets.

@IJEMIN
Created January 20, 2018 12:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IJEMIN/d62b2d85816bedf7c80b810cd57e5f55 to your computer and use it in GitHub Desktop.
Save IJEMIN/d62b2d85816bedf7c80b810cd57e5f55 to your computer and use it in GitHub Desktop.
How to Raycast by Angle in Unity
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");
}
}
}
@Valignus
Copy link

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.

@IJEMIN
Copy link
Author

IJEMIN commented Dec 21, 2022

@Valignus I'm Glad it helped! <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment