# DrawRay

> Draws a ray starting at a specified position from and extending in a given direction in the Scene view.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.7/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule

## DrawRay(Ray)

Draws a ray starting at a specified position `from` and extending in a given `direction` in the Scene view.

```csharp
public static void DrawRay(Ray r)
```

### Parameters

**** (\[Ray]\(/engine/6000.7/script-reference/unityengine/ray)): A Ray struct that contains the origin and direction of the ray to draw.

### Remarks

`DrawRay` is a method used for visual debugging in the Unity Editor. `DrawRay` renders a line segment in the Scene view. `DrawRay` is useful for visualizing raycasts, directions, or vectors in your scene.

The line is drawn using the current Gizmos color, which can be set using [Gizmos.color](/engine/6000.7/script-reference/unityengine/gizmos/color.md) before calling `DrawRay`. The line is drawn using the current Gizmos matrix, which can be set using [Gizmos.matrix](/engine/6000.7/script-reference/unityengine/gizmos/matrix.md) before calling `DrawRay`.

Typically, you'd use this method within [MonoBehaviour.OnDrawGizmos()](/engine/6000.7/script-reference/unityengine/monobehaviour/ondrawgizmos.md) or [MonoBehaviour.OnDrawGizmosSelected()](/engine/6000.7/script-reference/unityengine/monobehaviour/ondrawgizmosselected.md) functions in your MonoBehaviour scripts, or within a method with the [DrawGizmo](/engine/6000.7/script-reference/unityeditor/drawgizmo.md) 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](/engine/6000.7/script-reference/unityengine/debug/drawline.md).

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](/engine/6000.7/script-reference/unityengine/gizmos/matrix.md).

```csharp
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()](/engine/6000.7/script-reference/unityengine/monobehaviour/ondrawgizmos.md), [MonoBehaviour.OnDrawGizmosSelected()](/engine/6000.7/script-reference/unityengine/monobehaviour/ondrawgizmosselected.md), [DrawGizmo](/engine/6000.7/script-reference/unityeditor/drawgizmo.md), [Gizmos.color](/engine/6000.7/script-reference/unityengine/gizmos/color.md), [Gizmos.matrix](/engine/6000.7/script-reference/unityengine/gizmos/matrix.md).

## DrawRay(Vector3, Vector3)

Draws a ray starting at a specified position `from` and extending in a given `direction` in the Scene view.

```csharp
public static void DrawRay(Vector3 from, Vector3 direction)
```

### Parameters

**** (\[Vector3]\(/engine/6000.7/script-reference/unityengine/vector3)): The ray start position in world space.**** (\[Vector3]\(/engine/6000.7/script-reference/unityengine/vector3)): The vector in world space defining the direction and length of the ray. The magnitude of this vector determines the length of the drawn ray.

### Remarks

`DrawRay` is a method used for visual debugging in the Unity Editor. `DrawRay` renders a line segment in the Scene view. `DrawRay` is useful for visualizing raycasts, directions, or vectors in your scene.

The line is drawn using the current Gizmos color, which can be set using [Gizmos.color](/engine/6000.7/script-reference/unityengine/gizmos/color.md) before calling `DrawRay`. The line is drawn using the current Gizmos matrix, which can be set using [Gizmos.matrix](/engine/6000.7/script-reference/unityengine/gizmos/matrix.md) before calling `DrawRay`.

Typically, you'd use this method within [MonoBehaviour.OnDrawGizmos()](/engine/6000.7/script-reference/unityengine/monobehaviour/ondrawgizmos.md) or [MonoBehaviour.OnDrawGizmosSelected()](/engine/6000.7/script-reference/unityengine/monobehaviour/ondrawgizmosselected.md) functions in your MonoBehaviour scripts, or within a method with the [DrawGizmo](/engine/6000.7/script-reference/unityeditor/drawgizmo.md) 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](/engine/6000.7/script-reference/unityengine/debug/drawline.md).

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](/engine/6000.7/script-reference/unityengine/gizmos/matrix.md).

```csharp
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()](/engine/6000.7/script-reference/unityengine/monobehaviour/ondrawgizmos.md), [MonoBehaviour.OnDrawGizmosSelected()](/engine/6000.7/script-reference/unityengine/monobehaviour/ondrawgizmosselected.md), [DrawGizmo](/engine/6000.7/script-reference/unityeditor/drawgizmo.md), [Gizmos.color](/engine/6000.7/script-reference/unityengine/gizmos/color.md), [Gizmos.matrix](/engine/6000.7/script-reference/unityengine/gizmos/matrix.md).
