查看其他标签但不查看他自己的标签

发布于 2024-10-24 04:14:51 字数 749 浏览 2 评论 0原文

是否可以调用数组中的所有对象?或者使用该号码是唯一的方法吗?

public void Update()
{
    Targetting ge = (Targetting)target.GetComponent("Targetting");
    enemies = ge.targets;

    float attackDistanceP = Vector3.Distance(target.transform.position, transform.position);

    foreach (Transform enemy in enemies)
    {
        float enemyDistance = Vector3.Distance(enemy.transform.position, transform.position);
        print(enemyDistance);
    }

    Debug.DrawLine(transform.position, waypointPosition, Color.black);

    if (attackDistanceP < 8)
        disableNow = true;

    if (attackDistanceP > 8)
        disableNow = false;

}

我想调用敌人[]数组中的所有对象。这样所有的敌人都能看到对方。但我不想让他们看到自己。我使用 GameObject.FindGameObjectsWithTag 来填充其他脚本中的数组。

Is it possible to call all the objects in an array? Or is using the number the only way?

public void Update()
{
    Targetting ge = (Targetting)target.GetComponent("Targetting");
    enemies = ge.targets;

    float attackDistanceP = Vector3.Distance(target.transform.position, transform.position);

    foreach (Transform enemy in enemies)
    {
        float enemyDistance = Vector3.Distance(enemy.transform.position, transform.position);
        print(enemyDistance);
    }

    Debug.DrawLine(transform.position, waypointPosition, Color.black);

    if (attackDistanceP < 8)
        disableNow = true;

    if (attackDistanceP > 8)
        disableNow = false;

}

I want to call all of the objects in the enemies[] array. So that all of the enemies can see each other. But I don't want to them to see themselfs. Im using the GameObject.FindGameObjectsWithTag to fill the array in an other script.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无戏配角 2024-10-31 04:14:51

为您提供一些过早的优化:

using System.Collections.Generic;

public Transform trans;
Targetting ge;

void Start() {
    trans = transform;
    ge = target.GetComponent<Targetting>();
}

void Update () {
    enemies = ge.targets;

    float attackDistanceP = (target.trans.position - trans.position).sqrMagnitude; // save a square root

    foreach(Transform enemy in enemies){
        if (enemy == trans) continue; // skip me

        float enemyDistance = (enemy.trans.position - trans.position).sqrMagnitude; // save a square root
        print(enemyDistance);
    }

    Debug.DrawLine (trans.position, waypointPosition, Color.black);

    if(attackDistanceP < 64)
        disableNow = true;

    if(attackDistanceP > 64)
        disableNow = false;
}

Some premature optimization for you:

using System.Collections.Generic;

public Transform trans;
Targetting ge;

void Start() {
    trans = transform;
    ge = target.GetComponent<Targetting>();
}

void Update () {
    enemies = ge.targets;

    float attackDistanceP = (target.trans.position - trans.position).sqrMagnitude; // save a square root

    foreach(Transform enemy in enemies){
        if (enemy == trans) continue; // skip me

        float enemyDistance = (enemy.trans.position - trans.position).sqrMagnitude; // save a square root
        print(enemyDistance);
    }

    Debug.DrawLine (trans.position, waypointPosition, Color.black);

    if(attackDistanceP < 64)
        disableNow = true;

    if(attackDistanceP > 64)
        disableNow = false;
}
仅冇旳回忆 2024-10-31 04:14:51
foreach(Transform enemy in enemies){
    if (enemy == this.transform) continue; // skip me

    float enemyDistance = Vector3.Distance(enemy.transform.position, transform.position);
    print(enemyDistance);
}
foreach(Transform enemy in enemies){
    if (enemy == this.transform) continue; // skip me

    float enemyDistance = Vector3.Distance(enemy.transform.position, transform.position);
    print(enemyDistance);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文