[英]How do I assign a new transform.position for a GameObject that's triggered by an OnTriggerEnter in Unity?
[英]How do I assign a GameObject's x and y position?
我正在創建一個統一的視頻游戲,對於關卡選擇,我需要將GameObject的x和y位置設置為按鈕的x和y位置。
我試過這段代碼 -
if (gameObject.CompareTag("Level1")) {
float xPos = gameObject.transform.position.x;
float yPos = gameObject.transform.position.y;
levelWindow.SetActive(true);
levelTitle.text = "One- Dunes of Coral";
levelDescription.text = "Begin your ocean voyage in the safe haven of the Hawaiian coral reefs...";
levelWindow.transform.position.x = xPos;
levelWindow.transform.position.y = yPos;
}
但我得到一個像這樣的錯誤 -
Assets / Scripts / LevelTapScript.cs(21,39):錯誤CS1612:無法修改`UnityEngine.Transform.position'的值類型返回值。 考慮將值存儲在臨時變量中
我的問題是如何使用我的xPos和yPos浮點數設置levelWindow(這是一個游戲對象)的x和y位置? 謝謝 - 喬治:)
您必須創建一個臨時的Vector3
變量,修改x軸然后將其分配回Transform.position
。
if (gameObject.CompareTag("Level1"))
{
float xPos = gameObject.transform.position.x;
float yPos = gameObject.transform.position.y;
Vector3 newPos = new Vector3(xPos,yPos,0);
levelWindow.SetActive(true);
levelTitle.text = "One- Dunes of Coral";
levelDescription.text = "Begin your ocean voyage in the safe haven of the Hawaiian coral reefs...";
levelWindow.transform.position = newPos;
levelWindow.transform.position = newPos;
}
請注意,執行此操作時,z pos將為0。
兩件事情:
一,對於Unity問題,請考慮使用https://gamedev.stackexchange.com/
二,不能直接設置變換的位置值。 你需要做的是:
levelWindow.transform.position = new Vector3(xPos,yPos,0);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.