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

参数

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

描述

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

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

如果将 depthTest 设置为 true,则该直线将被此场景中更靠近摄像机的其他对象遮挡。

将在该 Editor 的场景视图中绘制该直线。如果在游戏视图中启用了辅助图标绘图,则在该视图中也将绘制该直线。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { void Update() { Vector3 forward = transform.TransformDirection(Vector3.forward) * 10; Debug.DrawRay(transform.position, forward, Color.green); } }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { void OnCollisionEnter(Collision collision) { foreach (ContactPoint contact in collision.contacts) { Debug.DrawRay(contact.point, contact.normal, Color.green, 2, false); } } }