How to make a GetKeyDown function work more than once?

I am using Unity 3D. I am trying to make a game where the gameobject is a cube. it starts in a certain position and I want it so that when the WASD keys are pressed then the cube teleports to a certain position. However, depending on the position it is already in, it will teleport to a different place. This would work out so that if its in a position and moves back by pressing S it then can move back again by pressing S rather than going to the original place it was in when I first press S. But when I run the game, the cube teleports to each position but then it won’t move again. Why do the GetKeyDown functions only work once.
Thank You

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour{

public Vector3 position;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
 if (Input.GetKeyDown(KeyCode.W)){
  if (transform.position.y == 0.70038f){
   if (transform.position.z == 0.2f){
     Vector3 temp = new Vector3(0, 0.70038f, -1.1f);
     gameObject.transform.position += temp;
     }
    }
   }
 else if (Input.GetKeyDown(KeyCode.S)){
  if (transform.position.y == 0.70038f){
    Vector3 temp = new Vector3(0, 0.70038f, 1.1f);
    gameObject.transform.position += temp;
    }
 }
 else if (Input.GetKeyDown(KeyCode.A)){
  if (transform.position.y == 0.70038f){

    Vector3 temp = new Vector3(1.3f, 0.70038f, 1.3f);
    gameObject.transform.position += temp;
    
  }
 }
}

}
,Here is the code I have written, it is attached to a cube which is the gameobject. I am trying to make it so that depending on where it is when you press one of the keys it teleports to another area but it only works once. So when I start the game, I can press any of the keys and it goes to the position it’s supposed to, but if I press any of the keys again nothing happens despite the fact the cube should be in the right position to fit the second if statements.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;



 public class Movement : MonoBehaviour{


    public Vector3 position;

    // Start is called before the first frame update
    void Start()
    {
  
    }

    // Update is called once per frame
    void Update()
    {
     if (Input.GetKeyDown(KeyCode.W)){
      if (transform.position.y == 0.70038f){
       if (transform.position.z == 0.2f){
         Vector3 temp = new Vector3(0, 0.70038f, -1.1f);
         gameObject.transform.position += temp;
         }
        }
       }
     else if (Input.GetKeyDown(KeyCode.S)){
      if (transform.position.y == 0.70038f){
        Vector3 temp = new Vector3(0, 0.70038f, 1.1f);
        gameObject.transform.position += temp;
        }
	 }
     else if (Input.GetKeyDown(KeyCode.A)){
      if (transform.position.y == 0.70038f){

        Vector3 temp = new Vector3(1.3f, 0.70038f, 1.3f);
        gameObject.transform.position += temp;
        
	  }
	 }
    }

}

GetKey/Button methods run multiple times i.e. every frame they are held. GetKey/ButtonDown are a convenience method for when you only want it to run once.


First of all I’d like to point out that comparing floating point numbers (especially when they are the result of a mathematical operation) is a bad idea and will sooner or later get you into all sorts of trouble. I highly recommend to watch this Numberphile video. Also note that the precision of floating point numbers gets worse the bigger the numbers get. See my table over here for reference.

Apart from that assuming your numbers turn out right, you do not teleport to a certain location, you actually add an offset to your current position. So assuming you hit the “W” key and your current position is actually y == 0.70038 and z == 0.2 so both if statements will evaluate to true. You now add the offset (0, 0.70038, -1.1) to your current position. That means your new position would be y == 1.40076 and z == -0.9.

Of course pressing the key (or any of your keys) again will fail your if statements since y is no longer 0.70038

Maybe you wanted to actually set the objects position to the vector you created. In that case you have to replace

transform.position += temp;

with

transform.position = temp;

You are using Inpyr.GetKey
It is better if you use GetKey it should work I also use this by holding down the button that is assigned

The best way to do that is by using Input.GetKey

 if (Input.GetKey(KeyCode."Your Key"))
        {
            //code
        } else
        {
            //code
        }