Questions about the for statement in Unity.

Asked 2 years ago, Updated 2 years ago, 75 views

I'm a beginner at Unity.

Questions about the for statement in Unity.
Put as many characters as you can see on the dice. I set up a program to move it.
I want to move it up, down, left, and right, one square at a time, but
Once the direction is specified, it moves in that direction at once.
How can I correct it so that I can move one square while waiting for input?
The contents of the program are as follows.
It is running in update.

Thank you for your cooperation.

d d62.lastresult is the eye of the dice.

public void MoveMain(){

    Dice2 d62 = dice2.GetComponent();
    if(d62.lastresult!=0&jj==false){
        for (inti=0;i<d62.lastresult;i++){
            if(Input.GetKeyUp(KeyCode.W){
                this.transform.position + = new Vector3(0,0,1);
                jj = true;
            } else if (Input.GetKeyUp(KeyCode.S)) {
                this.transform.position + = new Vector3(0,0,-1);
                jj = true;
            } else if (Input.GetKeyUp(KeyCode.A)) {
                this.transform.position + = new Vector3(-1,0,0);
                jj = true;
            } else if(Input.GetKeyUp(KeyCode.D)) {
                this.transform.position + = new Vector3(1,0,0);
                jj = true;
            }
        }
    }
}

c# unity3d

2022-09-30 21:13

2 Answers

public void MoveMain(){

    Dice2 d62 = dice2.GetComponent();
    if(d62.lastresult!=0&jj==false){
        for (inti=0;i<d62.lastresult;i++){
            if(Input.GetKeyUp(KeyCode.W){
                this.transform.position + = new Vector3(0,0,1);
                jj = true;
            } else if (Input.GetKeyUp(KeyCode.S)) {
                this.transform.position + = new Vector3(0,0,-1);
                jj = true;
            } else if (Input.GetKeyUp(KeyCode.A)) {
                this.transform.position + = new Vector3(-1,0,0);
                jj = true;
            } else if(Input.GetKeyUp(KeyCode.D)) {
                this.transform.position + = new Vector3(1,0,0);
                jj = true;
            }
        }
    }
}

With the above, we will process everything in the same frame in the first place.
Therefore, you need to manage it by frame.

1st frame:input
Second frame: (if input is detected) Move
3rd frame: (if input is detected) Move
·
·
·

So if you manage and control each event with a flag,
I think what the questioner wants to do can be realized.

add

Regarding the above code,

if(d62.lastresult!=0&jj==false){

Here is the branch if the dice result is non-zero and jj? is false.

if(Input.GetKeyUp(KeyCode.W){
else if (Input.GetKeyUp(KeyCode.S)) {
else if (Input.GetKeyUp(KeyCode.A)) {
else if (Input.GetKeyUp(KeyCode.D)) {

If one of these is true, the result is always true in the same frame.

jj = true;

I think this is supposed to serve as a stopper, but I can't.

You can use bullet points once, so try to design what you want to achieve in chronological order.
I will add more if necessary.


2022-09-30 21:13


Waiting for input in the routine Wouldn't it be possible to write it in the following way?

public void MoveMain(){
    StartCoroutine (CoMoveMain());
}

IEnumerator CoMoveMain() {
    Dice2 d62 = dice2.GetComponent();
    for (inti=0;i<d62.lastresult;i++){
        While(!Input.anyKeyDown) {yield return 0;}

        if(Input.GetKeyUp(KeyCode.W){
            this.transform.position + = new Vector3(0,0,1);
        } else if (Input.GetKeyUp(KeyCode.S)) {
            this.transform.position + = new Vector3(0,0,-1);
        } else if (Input.GetKeyUp(KeyCode.A)) {
            this.transform.position + = new Vector3(-1,0,0);
        } else if(Input.GetKeyUp(KeyCode.D)) {
            this.transform.position + = new Vector3(1,0,0);
        }
        else{
            i--;
        }
    }
}

T As Mr. Transam pointed it out, I added it.
...I wonder if it's like this (^^;?

)


2022-09-30 21:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.