Debug.DrawRay 不显示在编辑器上

0 unity-game-engine raycasting

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class collsionCheck : MonoBehaviour
{
    void Start()
    {

    }

    void Update()
    {
        Vector3 mouse = Input.mousePosition;
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit2D hit = Physics2D.Raycast(mouse, Vector3.zero);
            Debug.DrawRay(mouse, Vector3.zero, Color.green);
            if (hit)
            {
                Debug.Log("hello");
            }
        }
    }
}

Run Code Online (Sandbox Code Playgroud)

为什么它不起作用?,它没有向我显示错误。Gizmos 已启用,但我仍然看不到它。我是 Unity 的新手,所以这可能是一些愚蠢的事情。

Dig*_*hil 10

Debug.DrawRay()方法仅显示您是否设置了Gizmos选项设置为打开状态。

要在场景视图中查看它,请确保您已打开开关Gizmos场景视图

要在游戏视图中查看它,您必须执行相同的操作。 游戏视图

然而,在这里你使用的是

Debug.DrawRay(mouse, Vector3.zero, Color.green);

这里您定义的射线无效。我假设您想将光线从相机投射到游戏世界位置的某个点,这样您就可以执行以下操作Update()

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float distance = 100f;
Debug.DrawRay(ray.origin, ray.direction * distance, Color.green);
Run Code Online (Sandbox Code Playgroud)

另外,缓存Camera.main到变量中也是个好主意。