![]() If (key = GLFW_KEY_S & (action = GLFW_PRESS || action = GLFW_REPEAT)) Game::GetInstance()->cameraX -= cameraSpeed If (key = GLFW_KEY_A & (action = GLFW_PRESS || action = GLFW_REPEAT)) Game::GetInstance()->cameraY += cameraSpeed If (key = GLFW_KEY_W & (action = GLFW_PRESS || action = GLFW_REPEAT)) GlfwSetWindowShouldClose(window, GLFW_TRUE) If (key = GLFW_KEY_ESCAPE & (action = GLFW_PRESS || action = GLFW_REPEAT)) So let's say I press A to move left, it goes left, then I press W to go diagonally upper-left, instead it 'forgets' about A being pressed and goes upward. Instead it acts like knowing only about last inputted key. The problem is it doesn't handle inputting multiple keys at once, like W and A should move it diagonally upper-left direction. In short get rid of the entire if-else structure and just stick with the if, if for some reason you really want to stick with the if-else structure reversing the order in which you check everything will also do (e.g check for the double key presses first, and the single key presses after, since you break out of the if-else after the first positive hit) but i wouldn't recommend this approach.Īlso you don't have to return the active pos.I am trying to create basic camera movement with glfwSetKe圜allback function. by checking if A is NOT pressed you will prevent this code from If(Input.GetKey(Ke圜ode.W) & !Input.GetKey(Ke圜ode.A)) Or you could do something like this: if(Input.GetKey(Ke圜ode.W) & Input.GetKey(Ke圜ode.A)) The pro of this is that you can do both the A and W check in the same if statement. some logic for when both A and W are pressed some logic goes here for when A is pressed This will mean that you will first check if key A is pressed, and inside that if statement you can do another check to see if W is also pressed, for example: if(Input.GetKey(Ke圜ode.A)) What i would personally probably do is nest your if statements. There are multiple ways you can can go around this, each having their own pros and cons. Never reaching else if (Input.GetKey(Ke圜ode.W) & Input.GetKey(Ke圜ode.A)) Since you did actually press the W, which will perform the code inside this if statement and break out of all other cases. if you hit both the keys A and W it will go through your code and the first match it will find will beĪctivePos = new Vector3(Random.Range(0.95f, -0.95f), 0, Random.Range(4.5f, 6f)) return activePos Your code is being run sequential, meaning from top to bottom. The problem lies with the fact that you are using a lot of if-else statements. If (Input.GetKey(Ke圜ode.W) & Input.GetKey(Ke圜ode.A))Īs this is perfectly fine for what you are trying to do. This is the full method: public static Vector3 GetTargetPosition()ĪctivePos = new Vector3(Random.Range(0.95f, -0.95f), 0, Random.Range(1f, 2.5f)) ĪctivePos = new Vector3(Random.Range(0.95f, -0.95f), 0, Random.Range(2f, 4.5f)) Įlse if (Input.GetKey(Ke圜ode.S) & Input.GetKey(Ke圜ode.A))ĪctivePos = new Vector3(Random.Range(-2.7f, -0.95f), 0, Random.Range(1f, 2.5f)) Įlse if (Input.GetKey(Ke圜ode.S) & Input.GetKey(Ke圜ode.D))ĪctivePos = new Vector3(Random.Range(0.95f, 2.7f), 0, Random.Range(1f, 2.5f)) Įlse if (Input.GetKey(Ke圜ode.W) & Input.GetKey(Ke圜ode.A))Įlse if (Input.GetKey(Ke圜ode.W) & Input.GetKey(Ke圜ode.D))ĪctivePos = new Vector3(Random.Range(0.95f, 2.7f), 0, Random.Range(4.5f, 6f)) Įlse if (Input.GetKey(Ke圜ode.A) || (Input.GetKey(Ke圜ode.Q) & Input.GetKey(Ke圜ode.A)))Įlse if (Input.GetKey(Ke圜ode.D) || (Input.GetKey(Ke圜ode.Q) & Input.GetKey(Ke圜ode.D)))ĪctivePos = new Vector3(Random.Range(0.95f, 2.7f), 0, Random.Range(2f, 4.5f)) I want only this code to run when w&a are pressed together if (Input.GetKey(Ke圜ode.W) & Input.GetKey(Ke圜ode.A)) Then this if (Input.GetKey(Ke圜ode.A) || (Input.GetKey(Ke圜ode.Q) & Input.GetKey(Ke圜ode.A)))ĪctivePos = new Vector3(Random.Range(-2.7f, -0.95f), 0, Random.Range(2f, 4.5f)) Īnd this if (Input.GetKey(Ke圜ode.W) & Input.GetKey(Ke圜ode.A))ĪctivePos = new Vector3(Random.Range(-2.7f, -0.95f), 0, Random.Range(4.5f, 6f)) Generating random location works perfectly but the only problem is when I try to press combination keys, first this runs if (Input.GetKeyUp(Ke圜ode.W))ĪctivePos = new Vector3(Random.Range(0.95f, -0.95f), 0, Random.Range(4.5f, 6f)) When I click a&w then generate a random Vector3 between a range like this: new Vector3(Random.Range(-2.7f, -0.95f), 0, Random.Range(4.5f, 6f)) I want my transform to move to different location when only A is pressed and to another different location when A is pressed in combination with W or S. It seems a common problem but I am unable to comprehend it. I think the problem is related to Input.Getkey and when I try to use Input.GeyKeyDown it becomes nearly impossible to press them both at the same time. But when I press them together something weird happens. In my code I am trying to detect whether a key for example 'A' is pressed alone or is pressed with combination.
0 Comments
Leave a Reply. |