簡體   English   中英

如何更改實例化游戲對象的變換位置?

[英]How to change the transform position of an instantiated GameObject?

非常基本的初學者問題:我正在制作一個磚塊破壞 Unity 游戲,我希望我的實例化球在槳上產生並跟隨槳直到我單擊鼠標。 我希望我的代碼能夠工作,但它只是產生一個球並且在實例化時不會跟隨槳。 我已經嘗試將此代碼放在我的球預制腳本中,但我無法從預制中引用 GameObject 槳。

public bool isActive;
public GameObject ball;
public GameObject paddle;

// Start is called before the first frame update
void Start()
{
    isActive = false;
    SpawnBall();
}

// Update is called once per frame
void Update()
{
    if (isActive == false)
    {
        ball.transform.position = new Vector2(paddle.transform.position.x, paddle.transform.position.y);
    }
}
private void SpawnBall()
{
    // spawn a ball on top of the paddle
    Instantiate(ball, new Vector2(paddle.transform.position.x, paddle.transform.position.y + 0.4f), Quaternion.identity);
}

嘗試使用接受父級變換的 Insatiate 重載:

Instantiate(Object original, Vector2 position, Quaternion rotation, Transform parent);

所以你可以這樣做:

Instantiate(ball, new Vector2(paddle.transform.position.x, paddle.transform.position.y + 0.4f), Quaternion.identity, paddle.Transform)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM