I have this aiming script in which the gun moves to the left (and the sights align) when I click the right mouse button. But the problem is that it doesn’t move by the number of units I want. Either I’m not using the translation correctly or I’m using the wrong void because when I right-click, the gun keeps moving until I let go. Here’s the script:
There are numerous ways to approach this issue, the easiest might be using Update() and fetching the MouseButtonDown on (1) , then logging this to a Class variable (isAiming) as you had already started to do. The order of events matters if doing it this way.
// public bool isAiming = false;
// public bool wasAiming = false; // I test with double variable
void Update() { // Called every frame update
isAiming = Input.GetMouseButton(1); // Stays true if still down
if (!wasAiming && isAiming) // If we only started aiming
transform.Translate (Vector3.left * 1.24822076f);
if (wasAiming && !isAiming) // If we only stopped aiming
transform.Translate (Vector3.right * 1.24822076f);
wasAiming = isAiming;
}
Thus each is only called once, based on the state of wasAiming. (note that I have no clause of if both are true, or if both are false; therefore they’re never called multiple times.)