I recently started Unity and I ran into a problem with RayCasting. My code seems to work since the console gives me the information that the cube has been hit, but it doesn't show me the ray in the scene view nor the game view. The script I used is the following:
public class RayCast : MonoBehaviour
{
Ray ray;
public LayerMask layersToHit;
void Start()
{
ray = new Ray(transform.position, transform.forward);
Debug.DrawRay(ray.origin, ray.direction * 100f, Color.blue);
CheckForColliders();
}
void CheckForColliders()
{
if (Physics.Raycast(ray, out RaycastHit hit))
{
Debug.Log(hit.collider.gameObject.name " was hit ");
Debug.DrawRay(ray.origin, ray.direction * 10, Color.red, 10f);
}
}
}
Now I also selected the LayersToHit in Unity to hit the connectors which are attached to the little cubes. What is wrong with the code for actually drawing the rays on the screen? Or do I need to change anything in the settings? Thanks in advance!
edit: New code:
public class RayCast : MonoBehaviour
{
private Ray ray;
public LayerMask layersToHit;
void Start()
{
ray = new Ray(transform.position, transform.forward);
//Debug.DrawRay(ray.origin, ray.direction * 100f, Color.blue);
CheckForColliders();
}
void CheckForColliders()
{
if (Physics.Raycast(ray, out RaycastHit hit))
{
Debug.Log(hit.collider.gameObject.name " was hit ");
//Debug.DrawRay(ray.origin, ray.direction * 10, Color.red, 10f);
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawRay(ray.origin, ray.direction * 100f);
}
}
CodePudding user response:
I tested your code and it works fine in my computer. So I think the problem might be a setting. Explanation:
void Start()
{
ray = new Ray(transform.position, transform.forward);
/* Here you are drawing the first ray, you did not have a duration set
for this one */
Debug.DrawRay(ray.origin, ray.direction * 100f, Color.blue, 10f);
CheckForColliders();
}
void CheckForColliders()
{
if (Physics.Raycast(ray, out RaycastHit hit))
{
Debug.Log(hit.collider.gameObject.name " was hit ");
// Here you are drawing the line again.
Debug.DrawRay(ray.origin, ray.direction * 10, Color.red, 10f);
}
}
This code runs fine but I´m not sure if you intended to draw the line twice, I presume the problem is the one I describe below, in the following link there is an image that shows the setting you may not have enabled and also shows the line being drawn.
Check if the draw Gizmos button on the top right of the scene view window is enabled.
Also if you want to be able to see the Ray without running the game your code should look like this:
private Ray ray;
public LayerMask layersToHit;
void Start()
{
ray = new Ray(transform.position, transform.forward);
//Debug.DrawRay(ray.origin, ray.direction * 100f, Color.blue);
CheckForColliders();
}
void CheckForColliders()
{
if (Physics.Raycast(ray, out RaycastHit hit))
{
Debug.Log(hit.collider.gameObject.name " was hit ");
//Debug.DrawRay(ray.origin, ray.direction * 10, Color.red, 10f);
}
}
private void OnDrawGizmos()
{
ray = new Ray(transform.position, transform.foward);
Gizmos.color= Color.red;
Gizmos.DrawRay(ray.origin, ray.direction * 100f);
}