Question by
Quentin-S · May 20, 2017 at 03:10 AM ·
raycastraycastingtilttilting
Raycast not working on tilted plane
Hi everyone,
I have a cube floating over a plane. I put 2 raycasts on the bottom of the cube in order to measure the distance between the cube and the plane. It works when both the cube and the plane are horizontally placed. But when I tilt the plane, it doesn't work anymore. The distances are the same when they should not be. Here is my C# code
public class Raycast : MonoBehaviour {
float d1, d2, D;
double alpha, beta;
void FixedUpdate()
{
RaycastHit hit;
Vector3 p1 = new Vector3(2.0f, -0.5f, 0);
Vector3 p2 = new Vector3(-2.0f, -0.5f, 0);
if (Physics.Raycast(p1, Vector3.up, out hit))
{
Debug.DrawRay(new Vector3(2.0f, -0.5f, 0), Vector3.up, Color.green);
print("Found an object from p1- distance: " + hit.distance);
d1 = hit.distance;
}
if (Physics.Raycast(p2, Vector3.up, out hit))
{
Debug.DrawRay(new Vector3(-2.0f,-0.5f,0), Vector3.up, Color.red);
print("Found an object from p2 - distance: " + hit.distance);
d2 = hit.distance;
}
D = Math.Abs(d2 - d1);
Debug.Log("D : " + D);
alpha = Math.Atan(4/D);
Debug.Log("alpha : " + alpha);
beta = 90 - alpha;
Debug.Log("beta : " + beta);
}
Thanks
Comment
Your answer
Follow this Question
Related Questions
Raycast not working with tilted plane 0 Answers
Raycasting else not working properly 0 Answers
Raycast Troubleshooting 1 Answer
Can't call raycast on plane to get mouse position? Beginner Question 1 Answer