【问题标题】:Unity: Raycast in Editor Mode does not workUnity:编辑器模式下的 Raycast 不起作用
【发布时间】:2016-12-29 12:38:40
【问题描述】:

我正在尝试在我的游戏中渲染球运动的轨迹路径,全部在编辑器模式下。

我相信这将有助于我的团队创建关卡,因为在制作原型时需要更少的测试。

这是我的代码:

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

问题是,我什至从未见过 Debug.Log("bounce");出现在控制台中。

换句话说,光线投射永远不会检测到任何东西。

我为此阅读了几十篇文章,其中很多都指出在编辑器模式下无法进行碰撞检测。

这是真的吗?或者我有我没有检测到的错误。

如果您需要更多信息,请告诉我,但我相信就是这样。

提前致谢,

【问题讨论】:

    标签: c# unity3d collision-detection raycasting collider


    【解决方案1】:

    图层蒙版不同于图层索引。图层掩码是一个由零和一组成的二进制数,其中每个1 代表一个包含的图层索引。换句话说,图层掩码中的ith 1 告诉unity 包含图层索引#i

    例如,如果图层蒙版应包括图层#0 和图层#2,则等于...000000101 = 5

    所以你必须换行

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

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

    其他数学信息

    &lt;&lt; 运算符将 LHS 左移 RHS 位。

    LHS:左侧

    RHS:右手边

    例如当我写1 &lt;&lt; 3 时,这意味着取1 并将其3 位向左移动。这给出了00001 &lt;&lt; 3 = 01000

    图层蒙版按位工作,您必须通过简单地设置(1)或重置(0)图层蒙版中相应的二进制数字来指定应屏蔽哪些图层以及应忽略哪些图层。

    如果我将图层蒙版设置为 7,则 unity 会将其转换为二进制 (00000111) 并屏蔽前三层并忽略所有其他图层。

    如果我将图层蒙版设置为 8,则 unity 会将其转换为二进制 (00001000) 并屏蔽第 4 层并忽略所有其他图层。

    您可以在herehere 阅读更多相关信息

    【讨论】:

    • 但我不明白会发生什么。介意评价一下。或者更好的是,与我分享一些链接,我可以在其中阅读更多相关信息。不过我会自己谷歌,但如果你有时间给我指个地方就太好了:)
    • 华丽,现在完全得到它。谢谢比扬,节日快乐^_^
    • 我不完全明白为什么在编辑器中我们必须添加 1
    • @UgoHed 我还没有遇到过其他方法有效的情况,我想知道如何。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    • 2018-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多