problem with lift script

well actually i’ve got a problem with my logical thinking because i’m new with JS, i want to use one input that runs two actions instead of two inputs. so what i want is when i press the F key the lift will play animation “LiftUp” and when i press F key again it will play animation “LiftDown”. here’s the script…i really appreciate any kind of suggestions/corrections from the members, thx

var liftSound : AudioClip;  
var EnterTrigger : boolean = false;
    							
function OnTriggerEnter (Col : Collider){
    if(Col.gameObject.tag == "Player"){
       EnterTrigger = true;          
    }
}
    
function OnTriggerExit (Col : Collider){
    if(Col.gameObject.tag == "Player"){
        EnterTrigger = false;             
    }
}
    
function Update () {
   if( EnterTrigger == true ){
      if(Input.GetKey(KeyCode.F)){
          audio.PlayOneShot(liftSound, 1.0 / audio.volume); 
          animation.Play("LiftUp");
      }
   }
   if( EnterTrigger == true ){
      if(Input.GetKey(KeyCode.G)){
          audio.PlayOneShot(liftSound, 1.0 / audio.volume);
          animation.Play("LiftDown");
      }
   }
}

2 Answers

2

Use a bool(ean). If the lift is down, the bool is false and when you press the F-key you check that bool. If the bool is false (that means the lift is down) you play the animation to go up, if the bool is true (means the lift is up) you play the animation to go down.

thanx, i will try that

var pressed:boolean;
function Update () {
if(Input.GetKey(KeyCode.F) && EnterTrigger){
audio.PlayOneShot(liftSound, 1.0 / audio.volume);
if(!pressed){
animation.Play(“LiftUp”);
pressed = true;
}else{
animation.Play(“LiftDown”);
pressed = false;
}
}
}

First checks, if input is used, then checks if the guy is in. At that point the sound is played then another checks occurs on whether it is already up or down and performs the appropriate action.