good day, trying to move an object x by 1 but when I press the arrow the object moves and returns to its place

public class rotation : MonoBehaviour { private Vector3 MousePos; private float myAngle = 0f; public float sensitivity = 1F; // Update is called once per frame void Update() { MousePos = Input.mousePosition; Camera main = GetComponent<Camera>(); Vector3 vector = new Vector3(1, 1, 1); if (Input.GetKeyDown(KeyCode.LeftArrow)) { float x = vector.x; float y = vector.y; float z = vector.z; x--; main.transform.position = new Vector3(x, y, z); } else { main.transform.position = vector; } } // Use this for initialization public GameObject go; private Camera goCamera; void Start() { goCamera = GetComponent<Camera>(); } 
  • one line if (Input.GetKeyDown (KeyCode.LeftArrow)) transform.Translate (-1, 0, 0); - Xumera_hZ

1 answer 1

 void Update() //Данный метод вызывается каждый кадр { Vector3 vector = new Vector3(1, 1, 1); //здесь вы объявили вектор (1,1,1) if (Input.GetKeyDown(KeyCode.LeftArrow)) //если кнопку нажали в этом кадре выполняется то, что указано ниже. { float x = vector.x; float y = vector.y; float z = vector.z; x--; main.transform.position = new Vector3(x, y, z); } else //если кнопка не была нажата в этом кадре, то позиция объекта устанавливается в точку (1,1,1), т.к. это значение переменной vector { main.transform.position = vector; } 

As a result, every frame you have is checking whether the button was pressed in this frame. If a position is clicked, it is set according to your transformations; if not, it is set to the vector point, which you previously declared as new Vector3 (1, 1, 1);