Unity:编辑器模式下的Raycast不起作用。

3
我正在尝试在编辑器模式下渲染游戏中球的运动轨迹路径。我相信这将有助于我的团队创建关卡,因为原型设计时需要进行的测试会更少。以下是我的代码:
using UnityEngine;
using UnityEditor;
using System.Collections;

namespace smthng {
    [ExecuteInEditMode]
    public class TrajectoryGenerator : MonoBehaviour
    {

        public int distance; //max distance for beam to travel.
        public LineRenderer lineRenderer; //the linerenderer that acts as trajectory path
        int limit = 100; // max reflections
        Vector2 curRot, curPos;


        void OnEnable()
        {

            StartCoroutine(drawLine());
        }

        IEnumerator drawLine()
        {
            int vertex = 1; //How many line segments are there
            bool isActive = true; //Is the reflecting loop active?

            Transform _transform = gameObject.transform;

            curPos = _transform.position; //starting position of the Ray
            curRot = _transform.right; //starting direction of the Ray


            lineRenderer.SetVertexCount(1);
            lineRenderer.SetPosition(0, curPos);

            while (isActive)
            {
                vertex++;
                //add ne vertex so the line can go in other direction
                lineRenderer.SetVertexCount(vertex); 

                //raycast to check for colliders
                RaycastHit2D hit = Physics2D.Raycast(curPos, curRot, Mathf.Infinity, LayerMask.NameToLayer(Constants.LayerHited));
                Debug.DrawRay(curPos, curRot, Color.black, 20);
                if (hit.collider != null)
                {
                    Debug.Log("bounce");
                    //position the last vertex where the hit occured
                    lineRenderer.SetPosition(vertex - 1, hit.point);
                    //update current position and direction;
                    curPos = hit.point;
                    curRot = Vector2.Reflect(curRot, hit.normal);
                }
                else
                {
                    Debug.Log("no bounce");
                    isActive = false;
                    lineRenderer.SetPosition(vertex - 1, curPos + 100 * curRot);

                }
                if (vertex > limit)
                {
                    isActive = false;
                }
                yield return null;
            }
        }
    }
}

问题是,我从未看到"bounce"在控制台中打印出来。

换句话说,射线投射从未检测到任何东西。

我阅读了许多有关此问题的文章,其中许多指出,在编辑器模式下无法检测碰撞器。

这是真的吗?还是我没有发现错误。

如果您需要更多信息,请告诉我,但我认为这就是全部内容。

提前致谢,

1个回答

3
Layer mask(层蒙版)和Layer index(层索引)是不同的。 Layer mask 是一个由零和一组成的二进制数,其中每个 1 表示一个包含的层索引。换句话说,第 i 个 1 在 Layer mask 中告诉 Unity 包括第 i 个层索引。
例如,如果要包括 layer #0 和 layer #2,则该层蒙版应等于...000000101 = 5。
因此,您必须更改以下行:
RaycastHit2D hit = Physics2D.Raycast(curPos, curRot, Mathf.Infinity, LayerMask.NameToLayer(Constants.LayerHited));

to

RaycastHit2D hit = Physics2D.Raycast(curPos, curRot, Mathf.Infinity, 1 << LayerMask.NameToLayer(Constants.LayerHited));

附加数学信息

<< 运算符将左侧的位移 RHS 位向左。

LHS:左侧

RHS:右侧

例如,当我写1 << 3时,它的意思是取1并将其向左移动3位。这将得到00001 << 3 = 01000

图层掩码按位运算,您必须指定哪些图层应该被掩盖和哪些图层应该被忽略,只需在图层掩码中设置(1)或重置(0)它们对应的二进制数字即可。

如果我将我的图层掩码设置为7,则Unity会将其转换为二进制数(00000111),并遮蔽前三层并忽略所有其他层。

如果我将我的图层掩码设置为8,则 Unity 将其转换为二进制数(00001000),并遮盖第四层并忽略所有其他层。

您可以在此处此处 阅读更多相关信息。


但我不明白发生了什么。你介意评估一下吗?或者更好的是,与我分享一些可以阅读更多关于此的链接。虽然我会自己谷歌搜索,但如果您有时间指点我去哪里,那就太棒了 :) - Kokolo
1
非常棒,现在完全明白了。谢谢Bijan,节日快乐^_^ - Kokolo
我不完全理解为什么在编辑器中我们要添加1 <<而不是在播放模式下,但它有效! - Ugo Hed
1
@UgoHed 我还没有遇到过其他方法可行的情况,我很好奇是怎么做到的。 - Bizhan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接