C# | What void do I put in if I want to do something once each click?
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:
 public bool isAiming = false;
  
          if(isAiming = false && Input.GetKey(KeyCode.Mouse1)){
              transform.Translate (Vector3.left * 1.24822076f);
              isAiming = true;
          }
          if(isAiming = true && Input.GetKey(KeyCode.Mouse1)){
                  transform.Translate (Vector3.right * 1.24822076f);
                  isAiming = false;
              }
 
              Answer by AlbinoStoic · Apr 29, 2016 at 01:22 AM
Hello!
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.)
Alright, I did this and it worked, but it didn't move by the amount I wanted. It moved 0.4 more.
You should generally not use the Translate to move a specific amount, you should ins$$anonymous$$d have a GunClose and a GunFar "Empty" attached to the player in the positions you want it to be.
Then you can move to the positions of these empties.
Okay, and how would I do that? Would it be:
 GameObject.transform.position = otherGameobject?
                   Answer by $$anonymous$$ · Apr 29, 2016 at 07:36 PM
The main mistake in the code is using = instead of == in the if-statements
Your answer