我似乎无法让光线投射到网格对撞机上 . 我需要这个来获得纹理uv坐标 .
if (Input.GetMouseButtonUp(0))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100f))
{
// We've hit this mesh b/c its the only one we have ...
var texture = meshRenderer.material.mainTexture as Texture2D;
var pixelUV = hit.textureCoord;
print("pixs uvs" + pixelUV.ToString());
pixelUV.x *= texture.width;
pixelUV.y *= texture.height;
var position = new Position(pixelUV.x, pixelUV.y);
print("Position: " + position.x + " " + position.y);
texture.FloodFillBorder(position.x, position.y, fillColor, borderColor);
texture.Apply();
}
}
我的游戏对象有这个脚本,更新函数检查输入,以及网格过滤器,网格渲染器和网格对撞机 . 网格是一个简单的四边形,由unity的菜单组成 .
我究竟做错了什么?我只是不明白它为什么没有击中网格 . 相机的 z position
是 -10
,网格位于 0
. 放置不同会产生相同的结果 .
1 回答
您可以检查网格组件并查看是否选中了“IsTrigger”,但我可以提出其他建议吗?
默认情况下,Unity会为您完成此光线投射 . MonoBehaviour类具有OnMouseDown()或onMouseDrag()等函数,其中GUIElement或在您的情况下具有Collider组件的对象获取这些事件 . 因此,如果您的游戏对象具有碰撞器组件,则只需实施其中一种方法,即可开始使用 .
这可以保持您的更新功能清洁 .
检查:http://docs.unity3d.com/ScriptReference/MonoBehaviour.html