- Home /
Raycast in Editor Mode does not work
Hi,
I am trying to render the trajectory path of the ball in my game, in Edit mode. I believe this will help my team in creating levels, as it will require less testing while prototyping.
Here is my code:
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;
}
}
}
}
The problem is, I have never even saw the Debug.Log("bounce"); appear in the console. In other words, the raycast never detects anything.
I read dozen of articles for this, many pointing that collider detection is impossible in editor mode.
Is this true? Or I have bug that I am not detecting.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612103645im_/https://answers.unity.com/themes/thub/images/avi.jpg)