I have a simple logic to detect any colliders in between Physics Raycast but it is not detecting any even if it has Colliders
public class RayCastBetweenPoints : MonoBehaviour
{
public Transform startPoint;
public Transform endPoint;
public LayerMask layerMask; // Optional: Specify which layers to include in the raycast
void Update()
{
if (startPoint != null && endPoint != null)
{
// Calculate the direction and distance of the ray
Vector3 direction = (endPoint.position - startPoint.position).normalized;
float distance = Vector3.Distance(startPoint.position, endPoint.position);
// Perform the raycast
RaycastHit hit;
if (Physics.Raycast(startPoint.position, direction, out hit, distance, layerMask))
{
// Log the name of the object hit
Debug.DrawLine(startPoint.position, endPoint.position, Color.yellow);
Debug.Log("Hit object: " + hit.collider.gameObject.name);
}
else
{
// Draw the ray in the Scene view for debugging
Debug.DrawLine(startPoint.position, endPoint.position, Color.red);
}
}
else
{
Debug.LogWarning("StartPoint or EndPoint is not assigned.");
}
}
}
The red color ray showing not detecting anything.
what am I missing.
The two start and end points have colliders disabled, only the middle one has.
Collision matrix also looks fine
Interestingly, the same code/logic works in different project but not in this project