I want to realize a turn-based attack in the battle scene of the RPG game.

Asked 2 years ago, Updated 2 years ago, 100 views

Nice to meet you.
I started studying Unity last month, and now that I'm done, I'm trying to create an RPG battle scene.

Below are the questions.


·Three friendly characters (A, B, C) and one enemy character (D).
·The order of attacks is A→B→C→D

[Question details]

This time, we created the following programs using if, switch, and for statements.

void Update(){
 for(n=1;n<5;n++){
  switch(n){
   case1:
    if(Input.GetKeyDown(KeyCode.P)){
     Debug.Log("A attack");
     break;
    }
   case2:
    if(Input.GetKeyDown(KeyCode.P)){
     Debug.Log("Batack");
     break;
    }
   case3:
    if(Input.GetKeyDown(KeyCode.P)){
     Debug.Log("Catack");
     break;
    }
   case4:
    if(Input.GetKeyDown(KeyCode.P)){
     Debug.Log("Datack");
     break;
    }
  }
 }
}

Ideally, if you press the P button on A's battle turn, A will attack.
After that, when you press the P button on the battle turn of B, B attacks.C and D follow...

However, when I actually ran the program, when I pressed the P button, all cases of ABCD were executed, and "each character attack" was displayed on the Console screen.

I would like to do this one by one for each character. Could you please let me know?

[Workaround?]

As a result of researching this solution online, I was told that I would use Console.ReadKey(); and ConsoleKeyInfo, but when I tried, I got an error and got stuck.
(The error is "All compiler errors have to be fixed before you can enter playmode!)

Thank you for your cooperation.

c# unity3d unity2d

2022-09-30 17:21

2 Answers

First, I will explain how the script works in the question.
Update() is a function called every frame, so any action written in this function will be executed every frame.
The questioner's script calls the for statement in Update(), so all cases in switch(n)~ are executed in each frame.
Input.GetKeyDown() is true throughout one frame when the key is pressed, so
This script runs all the A-D cases.

Also, regarding the workaround, Unity does not use the function Console.ReadKey().
The error is probably because you are trying to call a function that does not exist (more precisely, Console does not exist).

I think it will be difficult for the turn system that the questioner wants to realize in the current form.
"If you search ""Unity Turn Battle"" and so on, you will find some easy-to-understand articles, so please consider them."


2022-09-30 17:21

Why don't you keep the current attack status (who is attacking) in the member variable and change the status every time you press the p button?

For example, how about the following code?

private intn=1;

void Update() {
  if(Input.GetKeyDown(KeyCode.P)){
    switch(n){
      case1:
        Debug.Log("A attack");
        break;
      case2:
        Debug.Log("Batack");
        break;
      case3:
        Debug.Log("Catack");
        break;
      case4:
        Debug.Log("Datack");
        break;
    }
    n++;
  }
}


2022-09-30 17:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.