1

我希望能够点击一个开关并让它滑到另一边。现在那个开关是一个带有我的代码的预制件。当我添加该预制件的副本时,所有这些副本都会移动而不是我单击的单个副本。

    // Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit, 100.0f))
        {
            PrintName(hit.transform.gameObject);
        }
    }
}

private void PrintName(GameObject go)
{
    print(go.name);
    transform.position += new Vector3(3, 0, 0);
}

}

4

1 回答 1

0

更新功能适用于所有预制件。基本上,当您将光线投射到某物上时,脚本附加的每个预制件都会运行 PrintName 函数。这就是所有预制件同时移动的原因。

ps:也许你应该 go.transform.position 而不是 transform.position

于 2021-12-11T22:42:41.153 回答