對于平臺游戲,我一直在嘗試撰寫一個腳本來檢查我的玩家是否低于某個 Y 等級。所以它可以重生到開始,但是我如何將 y lvl 放在變數中來檢查它?我想不通哈哈
uj5u.com熱心網友回復:
在 Update() 中運行類似:
if(player.transform.position.y < 1)
{
//do something
}
其中“玩家”是有問題的游戲物件。
uj5u.com熱心網友回復:
我假設您只想比較(==, <=, >=, 所有爵士樂都是我的意思,以防萬一您不知道) Y 值與 10 之類的值。這是容易的,你甚至不需要一個變數必然為這一點。
//For the object position relative to the world
if(transform.position.y == 10) //"transform" gives you acces to the transform component
{ //of the object the script is attached to
Debug.Log("MILK GANG");
}
//For the object position relative to its Parent Object
if(transform.localPosition.y == 10)
{
Debug.Log("MILK GANG");
}
如果要更改物件位置的值,則
transform.position = new Vector2(6, 9)//Nice
//BTW new Vector2 can be used if you dont
//want to assign a completely new variable
但是,如果您想獲得對它的參考(基本上是一個告訴代碼您談論此組件的變數)。
private Transform Trans;
void Awake() //Awake is called/being executed before the first frame so its
{ //better than void Start in this case
Trans = GetComponent<Transform>();
Trans.position = new Vector2(69, 420); //Nice
}
這是這樣做的代碼方式,但還有另一種使用Unity 的方式
[SerializeField] private Transform Trans;
//[SerializeField] makes the variable changeable in Unity even if it is private so you
//can just drag and drop on to this and you good to go
希望這會有所幫助,如果沒有,那么我試過 le
uj5u.com熱心網友回復:
您可以使用添加到游戲transform.position物件的腳本來檢查物件。
if(transform.position.y < ylvl)
{
//do something
}
哪里ylvl是您要檢查的高度的整數
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/316209.html
標籤:统一3d
