- Home /
3d raycasting in 2d world with perspective camera
Hi Guys,
this is a variation of a problem I've had with conditionaly making objects on a ceiling layer transparent when these objects are between camera and player. So what Im basicaly trying to do is to cast a ray in 3D from camera to a player and get all 2d colliders along the way. Now, Im using a 2D project so I cant use regular Raycast, what I end up using is Physics2D.GetRayIntersectionNonAlloc. I'm also using a perspective camera here and it seems that might be a problem because the ray doesnt hit the collider correctly (I see on the scene that there is something between camera and player on ceiling layer but the rayhitting returns nothing).
Here is more or less the code Im using for ray hittint :
// This is not a production code ;)
RaycastHit2D[] hits = new RaycastHit2D[SOMESIZE];
// Get a direction vector from camera to player.
Vector3 dir = playerTransform.position - Camera.main.transform.position;
// Cast a 3D ray from camera to the player and get all 2d colliders ON CEILING LAYER that are on the ray.
var hitCount = Physics2D.GetRayIntersectionNonAlloc(new
Ray(Camera.main.transform.position, dir), hits, 10f, Registry.Instance.CEILING_LAYER_MASK_ID);
// Iterate ovet the points and do sth.
for(int i = 0; i < hitCount; i++) {
// CAST SOME MAGIC!
}
// Here Im trying to raw a line to see why its not realy working, and the line seems a bit OFF - starting from the camera but not reaching the player... Debug.DrawLine(Camera.main.transform.position, coll.point, Color.yellow);
Any suggetsions would be great, thx!
Your answer
Follow this Question
Related Questions
Raycast ignore itself 5 Answers
Using unity 4.6 UI and raycast2D functionality 0 Answers
How to raycast through friendly objects? 1 Answer
Raycast Layermask doesn't ignore no layered Parent? 1 Answer
Raycast doesnt hit ground 2 Answers