I'm trying to move objects by keystroke in Unity, but how do I stop processing until keystroke?

Asked 2 years ago, Updated 2 years ago, 50 views

If it is a game that is supposed to move frame by frame like action, I think it would be good to add a function of movement processing to Update and loop the function at the frame update interval, but I can't stop processing until I enter a key, for example, like RPG character movement.
The sauce looks like this.

01:void Start(){
02: StartCoroutine (CoroutineSample());
03:}
04:
05:void Update() {
06:}
07: IEnumerator CoroutineSample(){
08: Waitwhile(!Input.anyKeyDown) {yield return 0;} // Stop processing until key is entered
09:
10: if (the action of the character moving up when the key of W is pressed)
11—Multiple else if (move left for A, down for S, and right for D when pressing other keys
12:
13:} // Close CoroutineSample() parentheses

However, now that you build it,

WaitWhile(!Input.anyKeyDown) {yield return 0;} requires (;).

The build does not succeed with an error statement.

Where should I change to make the build successful?

I would appreciate it if you could teach me

c# unity3d

2022-09-30 19:40

1 Answers

I think WaitWhile() is used incorrectly.
If you write it like below, it will probably work out as intended.

yield return new WaitWhile()=>!Input.anyKeyDown);

For more information on WaitWhile(), see Unity Script Reference.

Unity - Script Reference: WaitWhile
https://docs.unity3d.com/ja/current/ScriptReference/WaitWhile.html


2022-09-30 19:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.