有没有办法只实例化一个对象的一个实例



目前,我正在开发一款追索权管理游戏,玩家可以选择某个工具,例如火,并将其应用于瓷砖。这个脚本应该检查玩家是否点击了";森林;瓷砖与消防工具,但它实例化了许多草地瓷砖和错误的位置。我如何才能阻止玩家按住点击并只实例化一个对象?此外,如果有人知道瓷砖为什么出现在命中对象变换上方,那将不胜感激。

void CheckMouseDown()
{
if (Input.GetAxis("Fire1") != 0 && canClick == true)
{
print("yes");
canClick = false;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
// Casts the ray and get the first game object hit
Physics.Raycast(ray, out hit);
if (hit.collider.gameObject.CompareTag("Forest"))
{
if (gamerule.gm.IsBurning == true)
{
Instantiate(meadow, hit.transform);
}
}
}
else
{
canClick = true;
}

}

使用Input.getMouseButtonDown(0(检查是否在此帧中单击了鼠标左键。当按下按钮时,您当前的方法会连续激发,因此您会多次生成对象。我想canClick是用来防止这种情况的,所以我在这里删除了它。Unity文档对此有一些更深入的信息。

void CheckMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
print("yes");
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
// Casts the ray and get the first game object hit
Physics.Raycast(ray, out hit);
if (hit.collider.gameObject.CompareTag("Forest"))
{
if (gamerule.gm.IsBurning == true)
{
Instantiate(meadow, hit.transform);
}
}
}
}

相关内容

最新更新