DrawRay
Draws a ray starting at a specified position from and extending in a given direction in the Scene view.
Read time 3 minutesLast updated 10 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
DrawRay(Ray)
Draws a ray starting at a specified position and extending in a given in the Scene view.
fromdirectionpublic static void DrawRay(Ray r)
Parameters
A Ray struct that contains the origin and direction of the ray to draw.
Remarks
DrawRayDrawRayDrawRayThe line is drawn using the current Gizmos color, which can be set using Gizmos.color before calling . The line is drawn using the current Gizmos matrix, which can be set using Gizmos.matrix before calling .
DrawRayDrawRayTypically, you'd use this method within MonoBehaviour.OnDrawGizmos() or MonoBehaviour.OnDrawGizmosSelected() functions in your MonoBehaviour scripts, or within a method with the DrawGizmo attribute. Like all Gizmos, it's an Editor-only tool and should not be used for game functionality. This method has no effect in the GameView or at runtime. If you want to draw similar shapes at runtime refer to Debug.DrawLine.
The line segment respects the 3D space and perspective of the scene, including depth and occlusion by other objects. It also respects the alpha channel of the Gizmos color, allowing for semi-transparent lines as well as the transformation of Gizmos.matrix.
using UnityEngine;public class RayGizmoExample : MonoBehaviour{ public Vector3 direction = Vector3.forward; public float length = 5f; [Range(0f, 1f)] public float alpha = 0.75f; private void OnDrawGizmos() { // Set the color with custom alpha. Gizmos.color = new Color(0f, 1f, 0f, alpha); // Green with custom alpha // Draw the ray. Gizmos.DrawRay(transform.position, direction.normalized * length); // Draw a sphere at the end of the ray. Gizmos.DrawSphere(transform.position + direction.normalized * length, 0.1f); }}
Additional Resources: MonoBehaviour.OnDrawGizmos(), MonoBehaviour.OnDrawGizmosSelected(), DrawGizmo, Gizmos.color, Gizmos.matrix.
DrawRay(Vector3, Vector3)
Draws a ray starting at a specified position and extending in a given in the Scene view.
fromdirectionpublic static void DrawRay(Vector3 from, Vector3 direction)
Parameters
Remarks
DrawRayDrawRayDrawRayThe line is drawn using the current Gizmos color, which can be set using Gizmos.color before calling . The line is drawn using the current Gizmos matrix, which can be set using Gizmos.matrix before calling .
DrawRayDrawRayTypically, you'd use this method within MonoBehaviour.OnDrawGizmos() or MonoBehaviour.OnDrawGizmosSelected() functions in your MonoBehaviour scripts, or within a method with the DrawGizmo attribute. Like all Gizmos, it's an Editor-only tool and should not be used for game functionality. This method has no effect in the GameView or at runtime. If you want to draw similar shapes at runtime refer to Debug.DrawLine.
The line segment respects the 3D space and perspective of the scene, including depth and occlusion by other objects. It also respects the alpha channel of the Gizmos color, allowing for semi-transparent lines as well as the transformation of Gizmos.matrix.
using UnityEngine;public class RayGizmoExample : MonoBehaviour{ public Vector3 direction = Vector3.forward; public float length = 5f; [Range(0f, 1f)] public float alpha = 0.75f; private void OnDrawGizmos() { // Set the color with custom alpha. Gizmos.color = new Color(0f, 1f, 0f, alpha); // Green with custom alpha // Draw the ray. Gizmos.DrawRay(transform.position, direction.normalized * length); // Draw a sphere at the end of the ray. Gizmos.DrawSphere(transform.position + direction.normalized * length, 0.1f); }}
Additional Resources: MonoBehaviour.OnDrawGizmos(), MonoBehaviour.OnDrawGizmosSelected(), DrawGizmo, Gizmos.color, Gizmos.matrix.