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

Parameters

start应作为该射线起始点的世界空间中的点。
dir该射线的方向和长度。
color绘制的直线的颜色。
duration该直线的可见时长(单位为秒)。
depthTest该直线是否应被靠近此摄像机的其他对象遮挡?

Description

在世界坐标中绘制一条从 startstart + dir 的直线。

duration 参数确定在绘制该直线所在的帧之后该直线可见时长。如果持续时间为 0(默认值),则该直线被渲染 1 帧。

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.

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); } }
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); } }