Legacy Documentation: Version 2018.1 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Debug.DrawRay

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static method DrawRay(start: Vector3, dir: Vector3, color: Color = Color.white, duration: float = 0.0f, depthTest: bool = true): void;
public static void DrawRay(Vector3 start, Vector3 dir, Color color = Color.white, float duration = 0.0f, bool depthTest = true);

Parameters

startPoint in world space where the ray should start.
dirDirection and length of the ray.
colorColor of the drawn line.
durationHow long the line will be visible for (in seconds).
depthTestShould the line be obscured by other objects closer to the camera?

Description

Draws a line from start to start + dir in world coordinates.

The duration parameter determines how long the line will be visible after the frame it is drawn. If duration is 0 (the default) then the line is rendered 1 frame.

If depthTest is set to true then the line will be obscured by other objects in the scene that are nearer to the camera.

The line will be drawn in the scene view of the editor. If gizmo drawing is enabled in the game view, the line will also be drawn there.

#pragma strict
public class Example extends MonoBehaviour {
	// Frame update example: Draws a 10 meter long green line from the position for 1 frame.
	function Update() {
		var forward: Vector3 = transform.TransformDirection(Vector3.forward) * 10;
		Debug.DrawRay(transform.position, forward, Color.green);
	}
}
using UnityEngine;

public class Example : MonoBehaviour { // Frame update example: Draws a 10 meter long green line from the position for 1 frame. void Update() { Vector3 forward = transform.TransformDirection(Vector3.forward) * 10; Debug.DrawRay(transform.position, forward, Color.green); } }
#pragma strict
public class Example extends MonoBehaviour {
	// Event callback example: Debug-draw all contact points and normals for 2 seconds.
	function OnCollisionEnter(collision: Collision) {
		Debug.DrawRay(collision.contacts[0].point, collision.contacts[0].normal, Color.green, 2, false);
	}
}
using UnityEngine;

public class Example : MonoBehaviour { // Event callback example: Debug-draw all contact points and normals for 2 seconds. void OnCollisionEnter(Collision collision) { Debug.DrawRay(collision.contacts[0].point, collision.contacts[0].normal, Color.green, 2, false); } }
对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答