我试图计算相机正在看的地面位置(没有做光线跟踪,因为我知道相机的前向矢量和地面的高度)。我尝试使用点积来做这个,但我似乎仍然得到了错误的答案。这就是我用我测试的值所做的事情:
const float groundHeight = 0f;
Vector3 cameraPosition = new Vector3(0f, 3f, 0f);
Vector3 cameraForward = new Vector3(0f, -0.7f, 0.7f); //Unit vector
Vector3 positionOnGround = cameraPosition;
positionOnGround.y = groundHeight;
Vector3 cameraToGround = positionOnGround - cameraPosition;
float scalar = Vector3.Dot(cameraToGround, cameraForward);
Vector3 forwardToGround = cameraForward * scalar;
return cameraPosition + forwardToGround;
出于某种原因,当我正在寻找高度为0的东西时,这给了我0,1.5,1.5的位置。任何关于我做错的想法?
答案 0 :(得分:0)
相机位于C点。
相机正在看地面上的目标点(P)(你想找到P )
距离地面yC = camera.transform.position.y
地面相机投影(G)
p与G(zP)的距离
GCP的角度是。在你的情况下它是45度,因为CP是(0f,-0.7f,0.7f)
tan(a) = magnitude(PG) / magnitude(CG)
= zP / yC
= zP / camera.transform.position.y
P = new Vector3(xP,yP,zP)
= new Vector3(xC,0,zP)
= new Vector3(camera.transform.position.x, 0, tan(a)*camera.transform.position.y)