我正在制作一个运行时地形编辑器,但我的光线投射已损坏。如何在鼠标位置进行光线投射,然后获取可用于扩展地形的坐标?
我尝试了原始鼠标位置,这只是稍微不准确,然后我尝试了Input.GetAxis()and Camera.main.ScreenPointToRay()。
void Update ()
{
if (Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
print("Got Ray" + ray.ToString());
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.GetComponent<Terrain>())
{
Terrain terrain
hit.collider.gameObject.GetComponent<Terrain>();
TerrainData data = terrain.terrainData;
float height = data.GetHeight(
(int)Math.Round((double)Camera.main.ScreenToWorldPoint(Input.mousePosition).x),
(int)Math.Round((double)Camera.main.ScreenToWorldPoint(Input.mousePosition).y));
data.SetHeightsDelayLOD((int)Math.Round(
(double)Camera.main.ScreenToWorldPoint( Input.mousePosition).x),
(int)Math.Round((double)Camera.main.ScreenToWorldPoint(Input.mousePosition).y),
new float[3, 3]
{
{ height + 1.0f / 240000.0f, height + 1.0f / 240000.0f, height + 1.0f / 240000.0f },
{ height + 1.0f / 240000.0f, height + 1.0f / 240000.0f, height + 1.0f / 240000.0f },
{ height + 1.0f / 240000.0f, height + 1.0f / 2400.0f, height + 1.0f / 2400.0f }
});
terrain.ApplyDelayedHeightmapModification();
}
}
}
}
我预计地形会在鼠标位置增长,但它根本没有增长。