【发布时间】:2018-01-20 01:49:06
【问题描述】:
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 并且它没有根据鼠标光标位置移动,我仍然会看到光线投射激光。我正在移动鼠标光标,但光线投射激光似乎停留在同一个地方,所以我猜它只击中特定的地方/物体而不是所有东西?不知道发生了什么。
如果我使用的是 Mathf.Infinity,我根本看不到光线投射的激光。 当我在空间站周围移动鼠标光标时,只有当它检测到物体并打印它的名字时。
我想要做的是当我移动鼠标光标时,当它碰到空间站的任何物体时打印它的名字。
仅当我将这一行更改为 1000 时,我才会看到洋红色:
Debug.DrawRay(mapCamera.transform.position, mapCamera.transform.forward * 1000, Color.magenta);
如果是 Mathf.Infinity,我看不到任何洋红色激光。
【问题讨论】: