I have a world space canvas in front of the player and I want to shoot a ray towards an enemy (in the direction of the enemy) and create and object where Raycast hits the UI canvas.
I know that the physics engine doesn't interact with UI elements and event system is not the answer for me since it depends on pointer events and their position.
So how can I do this?
If possible, I would like to draw the ray or the line as gizmo to see if it's working.
My Current Code is this:
void OnDrawGizmos()
{
foreach (AeroplaneAiControl item in GameObject.FindObjectsOfType<AeroplaneAiControl>())
{
Gizmos.color = Color.magenta;
Gizmos.DrawLine(transform.position, item.transform.position);
Vector3 Direction = transform.position - item.transform.position;
RaycastHit rch;
Ray r = new Ray(transform.position, Direction);
Physics.Raycast(r, out rch);
if (rch.collider != null)
{
Gizmos.color = Color.cyan;
Gizmos.DrawLine(transform.position, rch.point);
Gizmos.DrawSphere(rch.point, 0.1f);
}
}
}
it draws the magenta line but not the cyan line and no spheres.