如何将不同的预制措施的位置更新为当前位置?

发布于 2025-01-31 14:33:04 字数 1207 浏览 4 评论 0原文

我正在制作一款游戏,使玩家是正方形,当他按键时它会改变形状。为此,我做了不同的预制,因为我有不同的改变。当我按键更改为一个预制键时,我将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终陌 2025-02-07 14:33:04

如果您希望将这些游戏对象的位置移至脚本对象的位置。最好尝试一下:

void Update()
{
    small.SetActive(false);
    normal.SetActive(false);
    big.SetActive(false);
    
    if (Input.GetKeyDown("q"))
    {
        big.SetActive(true);
        big.transform.position = transform.position;
    }
    else if (Input.GetKeyDown("w"))
    {
        normal.SetActive(true);
        normal.transform.position = transform.position;
    }
    else if (Input.GetKeyDown("e"))
    {
        small.SetActive(true);
        small.transform.position = transform.position;
    }
}

If you want the location of these gameObjects to be moved to the location of the script object. It is better to try this:

void Update()
{
    small.SetActive(false);
    normal.SetActive(false);
    big.SetActive(false);
    
    if (Input.GetKeyDown("q"))
    {
        big.SetActive(true);
        big.transform.position = transform.position;
    }
    else if (Input.GetKeyDown("w"))
    {
        normal.SetActive(true);
        normal.transform.position = transform.position;
    }
    else if (Input.GetKeyDown("e"))
    {
        small.SetActive(true);
        small.transform.position = transform.position;
    }
}
用心笑 2025-02-07 14:33:04

制作一个空的gameObject,然后将所有移动逻辑应用于它。将正常,小和大对象作为主要的gameObject的孩子,然后根据需要启用/禁用它们。

Make one empty GameObject and apply all movement logic to it. Put normal, small and big objects as children of the main GameObject and just enable/disable them as you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文