如何根据鼠标光标位置使用raycasthit?
How can I use raycasthit according to the mouse cursor position?
public Camera mapCamera;
private Camera[] cameras;
private GameObject mouseOvered;
void Update()
{
if (Input.GetKeyDown(KeyCode.M))
{
if (mapCamera.enabled == false)
{
foreach (Camera cam in cameras)
{
cam.enabled = false;
}
mapCamera.enabled = true;
}
else
{
foreach (Camera cam in cameras)
{
cam.enabled = true;
}
mapCamera.enabled = false;
}
}
bool rcHit = false;
Vector3 mouse = Input.mousePosition;
Ray castPoint = mapCamera.ScreenPointToRay(mouse);
RaycastHit hit;
Debug.DrawRay(mapCamera.transform.position,
mapCamera.transform.forward * Mathf.Infinity, Color.magenta);
if (Physics.Raycast(castPoint, out hit, Mathf.Infinity))
{
rcHit = true;
if (mouseOvered != hit.collider.gameObject)
{
mouseOvered = hit.collider.gameObject;
}
print(mouseOvered.name);
//do your thing here to apply/change the material
}
if (!rcHit && mouseOvered != null)
{
//do your thing to undo the material change
mouseOvered = null;
}
}
问题是当我在 运行 游戏中时,如果我将 Debug.DrawRay 形式 Mathf.Infinity 更改为 1000 并且它没有根据鼠标光标位置。我正在四处移动鼠标光标,但光线投射激光似乎停留在同一个地方,所以我猜它只击中特定的 places/objects 而不是所有的东西?不确定发生了什么。
如果我使用 Mathf.Infinity,我根本看不到光线投射的激光。
当我在 space 站周围移动鼠标光标时,只有在它检测到对象并打印它的名称时。
我想做的是当我移动鼠标光标时,当它击中 space 站的任何对象时打印它的名称。
例如,只有当我将此行更改为 1000 时,我才会看到洋红色:
Debug.DrawRay(mapCamera.transform.position, mapCamera.transform.forward * 1000, Color.magenta);
如果是 Mathf.Infinity 我没有看到激光的任何洋红色。
光线投射本身没有问题,问题出在你绘制的内容上。你不绘制 the 射线,而是 a "Ray" 从相机位置沿着它的前向矢量。
替换
Debug.DrawRay(mapCamera.transform.position, mapCamera.transform.forward * Mathf.Infinity, Color.magenta);
有
Debug.DrawRay(castPoint.origin, castPoint.direction * Matf.Infinity, Color.magenta);
我不确定乘以无穷大是否有效。大约 1000 个单位(甚至可能使用 mapCamera.farClipPlane
,因为玩家不太可能想在它之外挑选东西)对于大多数情况应该(超过)足够。
public Camera mapCamera;
private Camera[] cameras;
private GameObject mouseOvered;
void Update()
{
if (Input.GetKeyDown(KeyCode.M))
{
if (mapCamera.enabled == false)
{
foreach (Camera cam in cameras)
{
cam.enabled = false;
}
mapCamera.enabled = true;
}
else
{
foreach (Camera cam in cameras)
{
cam.enabled = true;
}
mapCamera.enabled = false;
}
}
bool rcHit = false;
Vector3 mouse = Input.mousePosition;
Ray castPoint = mapCamera.ScreenPointToRay(mouse);
RaycastHit hit;
Debug.DrawRay(mapCamera.transform.position,
mapCamera.transform.forward * Mathf.Infinity, Color.magenta);
if (Physics.Raycast(castPoint, out hit, Mathf.Infinity))
{
rcHit = true;
if (mouseOvered != hit.collider.gameObject)
{
mouseOvered = hit.collider.gameObject;
}
print(mouseOvered.name);
//do your thing here to apply/change the material
}
if (!rcHit && mouseOvered != null)
{
//do your thing to undo the material change
mouseOvered = null;
}
}
问题是当我在 运行 游戏中时,如果我将 Debug.DrawRay 形式 Mathf.Infinity 更改为 1000 并且它没有根据鼠标光标位置。我正在四处移动鼠标光标,但光线投射激光似乎停留在同一个地方,所以我猜它只击中特定的 places/objects 而不是所有的东西?不确定发生了什么。
如果我使用 Mathf.Infinity,我根本看不到光线投射的激光。 当我在 space 站周围移动鼠标光标时,只有在它检测到对象并打印它的名称时。
我想做的是当我移动鼠标光标时,当它击中 space 站的任何对象时打印它的名称。
例如,只有当我将此行更改为 1000 时,我才会看到洋红色:
Debug.DrawRay(mapCamera.transform.position, mapCamera.transform.forward * 1000, Color.magenta);
如果是 Mathf.Infinity 我没有看到激光的任何洋红色。
光线投射本身没有问题,问题出在你绘制的内容上。你不绘制 the 射线,而是 a "Ray" 从相机位置沿着它的前向矢量。
替换
Debug.DrawRay(mapCamera.transform.position, mapCamera.transform.forward * Mathf.Infinity, Color.magenta);
有
Debug.DrawRay(castPoint.origin, castPoint.direction * Matf.Infinity, Color.magenta);
我不确定乘以无穷大是否有效。大约 1000 个单位(甚至可能使用 mapCamera.farClipPlane
,因为玩家不太可能想在它之外挑选东西)对于大多数情况应该(超过)足够。