简体   繁体   English

Unity RayCast 确实命中了 object 但它没有显示在场景视图或游戏视图中

[英]Unity RayCast does hit the object but it is NOT showing in the scene view or game view

I recently started Unity and I ran into a problem with RayCasting.我最近开始使用 Unity,但遇到了 RayCasting 问题。 My code seems to work since the console gives me the information that the cube has been hit, but it doesn't show me the ray in the scene view nor the game view.我的代码似乎有效,因为控制台向我提供了立方体已被击中的信息,但它没有向我显示场景视图或游戏视图中的射线。 The script I used is the following:我使用的脚本如下:

public class RayCast : MonoBehaviour
{
    Ray ray;
    public LayerMask layersToHit;

    void Start()
    {
        ray = new Ray(transform.position, transform.forward);
        Debug.DrawRay(ray.origin, ray.direction * 100f, Color.blue);

        CheckForColliders();
    }

    void CheckForColliders()
    {
        if (Physics.Raycast(ray, out RaycastHit hit))
        {
            Debug.Log(hit.collider.gameObject.name + " was hit ");
            Debug.DrawRay(ray.origin, ray.direction * 10, Color.red, 10f);
        }
    }
}

Now I also selected the LayersToHit in Unity to hit the connectors which are attached to the little cubes.现在我还选择了 Unity 中的 LayersToHit 来点击连接到小立方体的连接器。 What is wrong with the code for actually drawing the rays on the screen?实际在屏幕上绘制光线的代码有什么问题? Or do I need to change anything in the settings?或者我需要更改设置中的任何内容吗? Thanks in advance!提前致谢!

edit: New code:编辑:新代码:

public class RayCast : MonoBehaviour
{
    private Ray ray;
    public LayerMask layersToHit;

    void Start()
    {
        ray = new Ray(transform.position, transform.forward);
        //Debug.DrawRay(ray.origin, ray.direction * 100f, Color.blue);
        CheckForColliders();
    }

    void CheckForColliders()
    {
        if (Physics.Raycast(ray, out RaycastHit hit))
        {
            Debug.Log(hit.collider.gameObject.name + " was hit ");
            //Debug.DrawRay(ray.origin, ray.direction * 10, Color.red, 10f);

        }
    }
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawRay(ray.origin, ray.direction * 100f);
    }
}

I tested your code and it works fine in my computer.我测试了你的代码,它在我的电脑上运行良好。 So I think the problem might be a setting.所以我认为问题可能出在设置上。 Explanation:解释:

void Start()
{
    ray = new Ray(transform.position, transform.forward);

    /* Here you are drawing the first ray, you did not have a duration set
    for this one */
    Debug.DrawRay(ray.origin, ray.direction * 100f, Color.blue, 10f);

    CheckForColliders();
}

void CheckForColliders()
{
    if (Physics.Raycast(ray, out RaycastHit hit))
    {
        Debug.Log(hit.collider.gameObject.name + " was hit ");

        // Here you are drawing the line again.
        Debug.DrawRay(ray.origin, ray.direction * 10, Color.red, 10f);
    }
}

This code runs fine but I´m not sure if you intended to draw the line twice, I presume the problem is the one I describe below, in the following link there is an image that shows the setting you may not have enabled and also shows the line being drawn.这段代码运行良好,但我不确定你是否打算画两次线,我认为问题出在我下面描述的问题,在下面的链接中有一张图片显示了你可能没有启用的设置并且还显示正在绘制的线。

Check if the draw Gizmos button on the top right of the scene view window is enabled.检查场景视图 window 右上角的 draw Gizmos 按钮是否启用。

Also if you want to be able to see the Ray without running the game your code should look like this:另外,如果你想在不运行游戏的情况下看到射线,你的代码应该如下所示:

private Ray ray;
public LayerMask layersToHit;


void Start()
{
    ray = new Ray(transform.position, transform.forward);
    //Debug.DrawRay(ray.origin, ray.direction * 100f, Color.blue);
    CheckForColliders();
}

void CheckForColliders()
{
    if (Physics.Raycast(ray, out RaycastHit hit))
    {
        Debug.Log(hit.collider.gameObject.name + " was hit ");
        //Debug.DrawRay(ray.origin, ray.direction * 10, Color.red, 10f);

    }
}
private void OnDrawGizmos()
{
    ray = new Ray(transform.position, transform.foward);
    Gizmos.color= Color.red;
    Gizmos.DrawRay(ray.origin, ray.direction * 100f);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM