C# 为什么Camera.ScreenPointToRay在3d世界中不精确?

C# 为什么Camera.ScreenPointToRay在3d世界中不精确?,c#,unity3d,C#,Unity3d,我试图将播放器悬停在光标上,但它无法正常工作,代码如下: var playerPlane = new Plane(Vector3.up, transform.position); // Generate a ray from the cursor position var ray = Camera.main.ScreenPointToRay(Input.mousePosition); // Determine the point where the cursor ray intersects

我试图将播放器悬停在光标上,但它无法正常工作,代码如下:

var playerPlane = new Plane(Vector3.up, transform.position);

// Generate a ray from the cursor position
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);

// Determine the point where the cursor ray intersects the plane.
// This will be the point that the object must look towards to be looking at the mouse.
// Raycasting to a Plane object only gives us a distance, so we'll have to take the distance,
//   then find the point along that ray that meets that distance.  This will be the point
//   to look at.
float hitdist = 0.0f;
// If the ray is parallel to the plane, Raycast will return false.
if (playerPlane.Raycast(ray, out hitdist)) {
    // Get the point along the ray that hits the calculated distance.
    var targetPoint = ray.GetPoint(hitdist);

    // Determine the target rotation.  This is the rotation if the transform looks at the target point.
    var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);

    // Smoothly rotate towards the target point.
    transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
这是玩家层次结构 玩家及其脚本:(进一步的子对象)

  • 网孔
  • 根据其父网格的旋转更改位置的同一网格

为什么要旋转它,而不是将其设置为光标点击点的位置?这不是您希望的方式。。。它需要一个介于
0
1
之间的值。。在您的情况下,您可能更希望使用固定值
0.5
,例如,目前您可能正试图通过使用FPS*0.5;)左右的值来尝试通过反复试验来找到一个值。)