How to make every if vypolnila at the same time?

0 like 0 dislike
47 views
Hello!
I'm working on a game in Unity (2D)
How to make sure that each if vypolnila "at the same time' and not from the top to the bottom ( I know that code is always executed from top to bottom, but how I can "bypass" )

What I need from the code: If I clamp and then press DownArrow ( for example ) press UpArrow, FireSphere was created on the last pressed key. ( Russian is my second language if I'm not clear obyasnila, I will try to fix the message ).

Code:
if (Input.GetButton("RightArrow") && Time.time > timeToFire) { timeToFire = Time.time + 1 / fireRate; Instantiate(fireSphere, firePoint.position, firePoint.rotation); } if (Input.GetButton("LeftArrow") && Time.time > timeToFire) { timeToFire = Time.time + 1 / fireRate; Instantiate(fireSphere, firePoint.position, firePoint.rotation); } if (Input.GetButton("UpArrow") && Time.time > timeToFire) { timeToFire = Time.time + 1 / fireRate; Instantiate(fireSphere, firePoint.position, firePoint.rotation); } if (Input.GetButton("DownArrow") && Time.time > timeToFire) { timeToFire = Time.time + 1 / fireRate; Instantiate(fireSphere, firePoint.position, firePoint.rotation); }


Thank you!
by | 47 views

2 Answers

0 like 0 dislike
Check not separate keys and combinations:
var isLeftPressed = Input.GetButton("LeftArrow"); var isUpPressed = Input.GetButton("UpArrow"); var isRightPressed = Input.GetButton("RightArrow"); var isDownPressed = Input.GetButton("DownArrow"); if (isUpPressed && isDownPressed && Time.time > timeToFire) { timeToFire = Time.time + 1 / fireRate; Instantiate(fireSphere, firePoint.position, firePoint.rotation); }
by
0 like 0 dislike
You must have only one place of establishment.
Ie, the thinking is this:
createBall = false if UP { if beforeDOWN { createBall = true } } if createBall { Instantiate(...) }
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked May 22, 2019 by EvgenyMorozov
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
33,945 users