- Home /
Plane.Raycast layerMask? Using both Physics and Plane raycast
I'm using a click to move controller script and trying to adjust it to work properly in a 3d world with varying heights, ledges, etc. Say the player is standing on a platform, and there's another platform to the left, but 5 units lower on the y axis. If the player moves his cursor straight left of his position and clicks to move on that lower platform, due to my camera's offset X angle the player doesn't actually click straight to his left (when attempting to move to the lower platform). I was able to correct this using a Plane, though Plane.Raycast doesn't have a layerMask parameter, so I'm Raycasting with a plane after the initial Physics raycast. I'm curious if anybody here could tell me if my code is correct, too expensive (I'm not sure how expensive raycasting really is), or maybe if there's a better way to do this.
If there's a way to use layerMask outside the Physics.Raycast parameter I think that would be ideal.
Ray ray = myCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray,out hit, rayDistance, layerMask))
{
playerPlane = new Plane(Vector3.up, myTransform.position);
float hitdist = 0f;
if(playerPlane.Raycast(ray, out hitdist))
currentMoveToPos = ray.GetPoint(hitdist);
else
currentMoveToPos = hit.point;
}
Your answer
Follow this Question
Related Questions
Raycast not detecting planes 4 Answers
Physics.SphereCastAll with triggers 1 Answer
Raycasting to the bottom side of a plane? 2 Answers
RayCast Help 1 Answer
Physics.CapsuleCast Help 1 Answer