C# Unity:编辑器模式下的光线投射不起作用
我试图在我的游戏中渲染球运动的轨迹路径,所有这些都是在编辑器模式下进行的 我相信这将有助于我的团队创建级别,因为在原型制作过程中需要更少的测试 这是我的密码:C# Unity:编辑器模式下的光线投射不起作用,c#,unity3d,collision-detection,raycasting,collider,C#,Unity3d,Collision Detection,Raycasting,Collider,我试图在我的游戏中渲染球运动的轨迹路径,所有这些都是在编辑器模式下进行的 我相信这将有助于我的团队创建级别,因为在原型制作过程中需要更少的测试 这是我的密码: using UnityEngine; using UnityEditor; using System.Collections; namespace smthng { [ExecuteInEditMode] public class TrajectoryGenerator : MonoBehaviour {
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”);出现在控制台中
换句话说,光线投射从未检测到任何东西
为此,我读了十几篇文章,许多文章指出,在编辑器模式下,对撞机检测是不可能的
这是真的吗?或者我有我没有检测到的bug
如果你需要更多信息,请告诉我,但我相信就是这样
提前感谢,层掩码与层索引不同。层掩码是由0和1组成的二进制数,其中每个
1
表示一个包含的层索引。换句话说,层掩码中的i
th1
告诉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但我不知道会发生什么。你介意评估一下吗。或者更好的是,与我分享一些链接,让我可以阅读更多关于这个的内容。虽然我会自己用谷歌搜索,但如果你有时间给我指个地方,那就太棒了:)太棒了,现在就完全明白了。谢谢Bijan,节日快乐^ ^我不完全理解为什么在编辑器中我们必须添加1@UgoHed,我还没有遇到其他任何东西都可以工作的情况,我想知道如何。
RaycastHit2D hit = Physics2D.Raycast(curPos, curRot, Mathf.Infinity, 1 << LayerMask.NameToLayer(Constants.LayerHited));