亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何讓一個游戲對象定位其他游戲對象的位置并告訴它們的顏色?

如何讓一個游戲對象定位其他游戲對象的位置并告訴它們的顏色?

C#
嗶嗶one 2022-11-13 15:52:40
我有 2 個游戲對象,一個藍色和一個紅色。兩人都在場景中跑來跑去,鏡頭受限,所以他們不能通過某個區域。如果可能的話,我也想知道如何在對角線上移動它們。這是我的全部代碼。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);}當我按下 G 和 V 時,我需要在控制臺上顯示它們的顏色和位置。C#unity3d
查看完整描述

1 回答

?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

您要查找的游戲對象的顏色屬性位于渲染器組件的材質中。


可以這樣引用:


gameObject.GetComponent<Renderer>().material.color

為了獲得 GameObject 的 Renderer 組件,您必須擁有對它的引用,例如:


GameObject object1;

GameObject object2;

然后,您可以像這樣獲取每個對象的顏色:


Color color1 = object1.GetComponent<Renderer>().material.color;

然后你可以像這樣檢查顏色的值:


if (color1 == Color.red){


    // Do something


}

或者,如果你想在控制臺顯示它們的顏色:


Debug.Log(color1);

顏色存儲為 4 個浮點值,因此如果要輸出“顏色為紅色”或“顏色為藍色”,則需要進行等價檢查,就像我在上面提供的“// 做某事”注釋中一樣。


查看完整回答
反對 回復 2022-11-13
  • 1 回答
  • 0 關注
  • 133 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號