- Home /
Gravity Switch by button?
Well i'm trying to make the gravity switch by press the "e" key. I had it working but players could simply spam the "e" button and fly. What can I do?
    function Update(){
         if (Input.GetKeyDown("e")) // 0 - left button; 1 - right button; 2 - middle button
         
             ThirdPersonController.gravity = -20;
 }
 function LateUpdate(){
         if (Input.GetKeyUp("e")); // 0 - left button; 1 - right button; 2 - middle button
         
             ThirdPersonController.gravity = 20;
             
 }
Answer by MaT227 · Jul 22, 2014 at 08:22 AM
You can make a timer to avoid player to push the button. Then once the player presses the button it must wait, let's say 5 seconds before pressing the button again.
You can simply create a timer inside your script and add a condition to your if statement.
Here is an example:
 // This is very simple pseudo code to illustrate what I mean
 var timer : float = 0.0f; // This is the timer
 var delay : float = 5.0f; // This is the delay in seconds
 function Update() // or FixedUpdate()
 {
     if (Input.GetKeyDown("e") && timer >= delay)
     {
         ThirdPersonController.gravity = 20 * (-1);
         timer = 0.0f
     }
     timer += Time.deltaTime;
 }
If this helped you don't forget to accept the answer. Thank you.
Your answer
 
 
             Follow this Question
Related Questions
Another Gravity Question: Player suddenly flies upward 1 Answer
Trying to figure out how to translate these gravity script to Javascript 2 Answers
Really simple IPhone and screen question 0 Answers
Make sure levels assets are loaded before switching? 1 Answer
musket gun script is not working 3 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                