如何将不同的预制措施的位置更新为当前位置?
我正在制作一款游戏,使玩家是正方形,当他按键时它会改变形状。为此,我做了不同的预制,因为我有不同的改变。当我按键更改为一个预制键时,我将Active Active the相应的预制设置,然后禁用另一个。问题在这里,我有三个预制,我不知道该连接在哪里更改位置运行时。我试图使该位置与其他位置相等,但它不起作用。
这里有我制作的代码:
public class Transformation : MonoBehaviour
{
public GameObject normal, small, big;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("q"))
{
big.SetActive(true);
normal.SetActive(false);
small.SetActive(false);
big.transform.position = normal.transform.position = small.transform.position;
}
if (Input.GetKeyDown("w"))
{
normal.SetActive(true);
big.SetActive(false);
small.SetActive(false);
normal.transform.position = big.transform.position = small.transform.position;
}
if (Input.GetKeyDown("e"))
{
small.SetActive(true);
normal.SetActive(false);
big.SetActive(false);
small.transform.position = big.transform.position = normal.transform.position;
}
}
}
I'm making a game where the player is a square and it changes shape when he presses a key. To do that I made different prefabs because I have different things to change. When I press the key to change to one prefab, I set active the corresponding prefab and I disable the other. The problem is here, I have three prefabs and I don't know where to connect to change the position runtime. I tried to make the position equal to the others but it didn't work.
Here there is the code I made:
public class Transformation : MonoBehaviour
{
public GameObject normal, small, big;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("q"))
{
big.SetActive(true);
normal.SetActive(false);
small.SetActive(false);
big.transform.position = normal.transform.position = small.transform.position;
}
if (Input.GetKeyDown("w"))
{
normal.SetActive(true);
big.SetActive(false);
small.SetActive(false);
normal.transform.position = big.transform.position = small.transform.position;
}
if (Input.GetKeyDown("e"))
{
small.SetActive(true);
normal.SetActive(false);
big.SetActive(false);
small.transform.position = big.transform.position = normal.transform.position;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望将这些游戏对象的位置移至脚本对象的位置。最好尝试一下:
If you want the location of these gameObjects to be moved to the location of the script object. It is better to try this:
制作一个空的
gameObject
,然后将所有移动逻辑应用于它。将正常,小和大对象作为主要的gameObject的孩子
,然后根据需要启用/禁用它们。Make one empty
GameObject
and apply all movement logic to it. Put normal, small and big objects as children of the mainGameObject
and just enable/disable them as you need.