Question by
SkydeRS · May 09, 2021 at 05:59 PM ·
cameraraycastmousepositionparent transform
ScreenPointToRay generate wrong ray because camera have a parent with transform
Hello, recently I try to create a box selection, from my RTS Camera, to detect entities inside this box.
I create a static function to get the position of the mouse position when she hit a specific collider but due to the parent transform attach to my camera, I can't get all entities inside the box selection.
My function :
public static Vector3 GetMousePosition(Collider coll, Vector3 screenPosition)
{
Vector3 position = Vector3.zero;
// maybe I have to apply some rotation here ?
Ray ray = Camera.main.ScreenPointToRay(screenPosition);
// Debug.DrawRay(ray.origin, ray.direction * 1000f, Color.red, 10f);
RaycastHit hit;
if (coll.Raycast(ray, out hit, 10000f))
{
position = hit.point;
}
return position;
}
By default, my camera have a parent with a rotation in Y axis to 45, when this rotation is to 0 I can catch all entites properly but with the default rotation I miss some entites and I don't find how I can avoid this issue :(
Thanks in advance :D
Comment