2

我正在尝试在我的游戏中渲染球运动的轨迹路径,所有这些都在编辑器模式下。

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

这是我的代码:

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"); 出现在控制台中。

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

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

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

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

提前致谢,

4

1 回答 1

3

图层蒙版不同于图层索引。图层掩码是一个由零和一组成的二进制数,其中每个1代表一个包含的图层索引。换句话说,图层掩码中的ith1告诉 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));

附加数学信息

<<运算符将 LHS 向左移动 RHS 位。

LHS:左侧

RHS:右手边

例如,当我写1 << 3它的意思是把13移到左边。这给出了00001 << 3=01000

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

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

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

您可以在此处此处阅读有关它的更多信息

于 2016-12-29T12:52:49.847 回答