如何使用统一的世界位置移动GameObjects?

时间:2016-10-24 21:08:25

标签: c# unity3d gameobject

所以,我试图弄清楚如何编写一个能够检测游戏对象何时处于特定世界位置的脚本,转换这些游戏对象并将其移动到1。

希望有意义,如果没有请求,我会尽力详细解释。非常感谢任何帮助。

2 个答案:

答案 0 :(得分:4)

void updateGameObjectsMatchingPosition(float x, float y, float z)
{
    // Please try to use something more specific like GameObject.FindGameObjectWithTag("CoolTag");
    var gameObjects : GameObject[] = GameObject .FindObjectsOfType(GameObject) as GameObject[];

    for(var i = 0; i < gameObjects.length; i++) {
       var position = gameObjects[i].transform.position;
       if(position.x == x && position.y == y && position.z == z) {
           //Do something with the position of this object.
           gameObjects[i].transform.position = new Vector3(x + 1, y + 1, z + 1);
       }
    }
}

答案 1 :(得分:1)

将此脚本附加到您的游戏对象。

[SerializedField] Vector3 _designatePosition;
IEnumerator MoveUp() {
    var speed = 5 * Time.deltaTime;
    var targetPosition = transform.position + new Vector3(0, 1, 0);
    while (Vector3.Distance(transform.position, targetPosition) > 0.01f) {
        transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed);
    }
}

void Update() {
    if (Vector3.Distance(transform.position, _designatePosition) < 0.01f) {
        StartCoroutine(MoveUp());
    }
}