我正在尝试在我的游戏中渲染球运动的轨迹路径,所有这些都在编辑器模式下。
我相信这将有助于我的团队创建关卡,因为在制作原型时需要更少的测试。
这是我的代码:
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"); 出现在控制台中。
换句话说,光线投射永远不会检测到任何东西。
我为此阅读了几十篇文章,其中很多都指出在编辑器模式下无法进行碰撞检测。
这是真的?或者我有我没有检测到的错误。
如果您需要更多信息,请告诉我,但我相信就是这样。
提前致谢,