简体   繁体   English

如何比较对象在数组中的位置?

[英]How do you compare a position of an object within an array?

so i was trying to compare a position of an object within an array with an int variable that i have所以我试图将数组中对象的位置与我拥有的 int 变量进行比较

I have this variable called stagePos, and i wanna use it to find an object that has the same value for its position in an array我有一个名为 stagePos 的变量,我想用它来查找一个对象,该对象在数组中的位置具有相同的值

If the stagePos is 1, then i want to find an object that has the position of [1] in an array and do something with it as well as do something with the other object in that array because they don't have the same value as stagePos如果 stagePos 为 1,那么我想在数组中找到一个位置为 [1] 的对象,并对其执行某些操作以及对该数组中的另一个对象执行某些操作,因为它们没有相同的值作为 stagePos

if(stage[i] != stage[stagePos]){
     Vector3 pos = stage[i].transform.position;
     pos += new Vector3(0f, -4f * Time.deltaTime , 0f);
     stage[i].transform.position = pos;
     stageScript.Invinsible(true);
}
else if(stage[i] == stage[stagePos]){
     Vector3 pos = stage[i].transform.position;
     pos += new Vector3(0f, 4f * Time.deltaTime, 0f);
     stage[i].transform.position = pos;
     stageScript.Invinsible(false);
}

i want the code to just sit there and wait until there's a change with the stagePos value我希望代码只是坐在那里等待直到 stagePos 值发生变化

So basically there is no issue in your code you just want it to be event driven instead of using Update .所以基本上你的代码没有问题,你只是希望它是事件驱动的而不是使用Update We don't see how and where exactly you change the value of stagePos but I would either use a method like我们不知道您如何以及在何处更改stagePos的值,但我会使用类似的方法

public void SetStagePos(int value)
{
    // ignore if same value
    if(stagePos == value) return;

    stagePos = value;

    for(var i = 0; i < stage.Length; i++)
    {
        // This could be actually done in one line
        stage[i].transform.position += new Vector3(0f,(i == stagePos ? 1 : -1) * 4f * Time.deltaTime , 0f);

        //Note: it is unclear where you want to go with the 
        //stageScript.Invisible()
        // since currently you anyway call it for all objects
        // so this will always end up with the value of
        // stageScript.Invisible(i == stage.Length - 1);
    }
}

This you would use like eg你会像这样使用例如

private void Update()
{
    if(Input.GetKeyDown(KeyCode.NumPad1))
    {
        SetStagePos(1);
    }
    //etc.
}

or alternatively make stagePos a property with backing field like或者将stagePos具有支持字段的属性,例如

private int _stagePos;
private int stagePos
{
    get => _stagePos;
    set =>
    {
        _stagePos = value;
        for(var i = 0; i < stage.Length; i++)
        {
            stage[i].transform.position += new Vector3(0f,(i == stagePos? 1 : -1) * 4f * Time.deltaTime , 0f);

            ....
        }
    }
}

Now everytime you assign a new value to stagePos the setter is called现在,每次您为stagePos分配一个新值时, stagePos调用 setter

private void Update()
{
    if(Input.GetKeyDown(KeyCode.NumPad1))
    {
        stagePos = 1;
    }
    //etc.
}

Note however that in general your usage of Time.deltaTime really makes only sense in the case this is called every frame!但是请注意,一般而言,您对Time.deltaTime的使用只有在每帧都被调用的情况下才有意义!

Depends ofcourse also on how you get the user input which you didn't show.当然也取决于您如何获得未显示的用户输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM