【问题标题】:How to make a gameobject locate others gameobjects positions and tell their color?如何让一个游戏对象定位其他游戏对象的位置并告诉他们的颜色?
【发布时间】:2019-03-27 19:25:18
【问题描述】:

我有 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 update
void Start()
{
    direction = SortdirectionGameObject(direction);
}

// Update is called once per frame
void 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】:

    您要查找的游戏对象的颜色属性位于渲染器组件的材质中。

    可以这样引用:

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

    为了得到一个游戏对象的渲染器组件,你必须有一个对它的引用,例如:

    GameObject object1;
    GameObject object2;
    

    然后您可以像这样获取每个对象的颜色:

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

    然后您可以像这样检查颜色的值:

    if (color1 == Color.red){
    
        // Do something
    
    }
    

    或者,如果您想在控制台上显示它们的颜色:

    Debug.Log(color1);
    

    颜色存储为 4 个浮点值,因此如果要输出“颜色为红色”或“颜色为蓝色”,则需要进行等价检查,就像我在上面提供的“// 做某事”注释中一样.

    【讨论】:

    • 虽然此代码可能会解决问题,including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请编辑您的答案以添加解释并说明适用的限制和假设。
    • 我知道,但我需要成为另一个游戏对象才能这样做。通过实例化一个预制件。
    • 编辑答案为您提供更多信息
    【解决方案2】:

    我不知道是否有获取 Gameobject 材质颜色的方法 但是你可以将 Gameobject 命名为它所具有的颜色,因为颜色是静态的并且不会改变,你可以使用 Gameobject.name 方法,并且对于定位其他可以使用 transform.Lookat 的游戏对象

    【讨论】:

    • public Transform 目标公众浮动速度; void FixedUpdate(){ transform.Lookat(target); transform.position+=transform.forward速度Time.deltaTime; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多