parent
69ae279f76
commit
42460a76fb
2 changed files with 36 additions and 0 deletions
@ -0,0 +1,27 @@ |
|||||||
|
use bevy::prelude::*; |
||||||
|
use bevy::window::{CursorGrabMode, CursorOptions}; |
||||||
|
|
||||||
|
pub fn debug_ray_cast( |
||||||
|
mut gizmos: Gizmos, |
||||||
|
mut ray_cast: MeshRayCast, |
||||||
|
window: Single<(&Window, &CursorOptions)>, |
||||||
|
camera: Single<(&GlobalTransform, &Camera)>, |
||||||
|
) { |
||||||
|
let (window, cursor) = window.into_inner(); |
||||||
|
let (transform, camera) = camera.into_inner(); |
||||||
|
|
||||||
|
let ray = if cursor.grab_mode == CursorGrabMode::Locked { |
||||||
|
Ray3d::new(transform.translation(), transform.forward()) |
||||||
|
} else if let Some(cursor_pos) = window.cursor_position() { |
||||||
|
camera.viewport_to_world(transform, cursor_pos).unwrap() |
||||||
|
} else { |
||||||
|
return; // cursor outside window area
|
||||||
|
}; |
||||||
|
|
||||||
|
let settings = &MeshRayCastSettings::default(); |
||||||
|
let Some((_entity, hit)) = ray_cast.cast_ray(ray, settings).first() else { |
||||||
|
return; // ray didn't hit anything
|
||||||
|
}; |
||||||
|
|
||||||
|
gizmos.arrow(hit.point, hit.point + hit.normal / 2., Color::WHITE); |
||||||
|
} |
||||||
Loading…
Reference in new issue