我有 2 個(gè)游戲?qū)ο螅粋€(gè)藍(lán)色和一個(gè)紅色。兩人都在場景中跑來跑去,鏡頭受限,所以他們不能通過某個(gè)區(qū)域。如果可能的話,我也想知道如何在對角線上移動(dòng)它們。這是我的全部代碼。public Transform Objectup;public Transform Objectdown;public Transform Objectright;public Transform Objectleft;public DirectionGameObject direction;public int speed = 2;public enum DirectionGameObject{ Right, Left, Up, Down}// Start is called before the first frame updatevoid Start(){ direction = SortdirectionGameObject(direction);}// Update is called once per framevoid Update(){ Destroy(); if(direction == DirectionGameObject.Right) { transform.Translate(Vector3.right * Time.deltaTime * speed); if(transform.position.x >= Objectright.position.x) { direction = SortdirectionGameObject(direction); } } if(direction == DirectionGameObject.Left) { transform.Translate(Vector3.left * Time.deltaTime * speed); if(transform.position.x <= Objectleft.position.x) { direction = SortdirectionGameObject(direction); } } if(direction == DirectionGameObject.Up) { transform.Translate(Vector3.up * Time.deltaTime * speed); if(transform.position.y >= Objectup.position.y) { direction = SortdirectionGameObject(direction); } } if(direction == DirectionGameObject.Down) { transform.Translate(Vector3.down * Time.deltaTime * speed); if(transform.position.y <= Objectdown.position.y) { direction = SortdirectionGameObject(direction); } }}private DirectionGameObject SortdirectionGameObject(DirectionGameObject maindirection){ DirectionGameObject directionSorted = maindirection; do{ int newDirection = Random.Range((int)DirectionGameObject.Right, (int)DirectionGameObject.Down + 1); directionSorted = (DirectionGameObject)newDirection; } while (maindirection == directionSorted); return directionSorted;}void Destroy(){ Destroy(gameObject,120.0f);}當(dāng)我按下 G 和 V 時(shí),我需要在控制臺上顯示它們的顏色和位置。C#unity3d
1 回答

守候你守候我
TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊
您要查找的游戲?qū)ο蟮念伾珜傩晕挥阡秩酒鹘M件的材質(zhì)中。
可以這樣引用:
gameObject.GetComponent<Renderer>().material.color
為了獲得 GameObject 的 Renderer 組件,您必須擁有對它的引用,例如:
GameObject object1;
GameObject object2;
然后,您可以像這樣獲取每個(gè)對象的顏色:
Color color1 = object1.GetComponent<Renderer>().material.color;
然后你可以像這樣檢查顏色的值:
if (color1 == Color.red){
// Do something
}
或者,如果你想在控制臺顯示它們的顏色:
Debug.Log(color1);
顏色存儲為 4 個(gè)浮點(diǎn)值,因此如果要輸出“顏色為紅色”或“顏色為藍(lán)色”,則需要進(jìn)行等價(jià)檢查,就像我在上面提供的“// 做某事”注釋中一樣。
- 1 回答
- 0 關(guān)注
- 126 瀏覽
添加回答
舉報(bào)
0/150
提交
取消