Advertisement
Guest User

Untitled

a guest
Jan 8th, 2018
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.74 KB | None | 0 0
  1.    
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.     // jumping
  10.         if(Input.GetKeyDown(KeyCode.Space) && !jumping && isGrounded)
  11.         {
  12.             jumping = true;
  13.             StartCoroutine(JumpRoutine());
  14.         }
  15.        
  16.  
  17.         // moving
  18.         rb.AddForce(transform.forward * Input.GetAxis("Vertical") * speed);
  19.         rb.AddForce(transform.right * Input.GetAxis("Horizontal") * speed);
  20.        
  21.  
  22.         // check if can jump
  23.         if (!isGrounded){
  24.             rb.AddForce(-transform.up * gravity);  
  25.         }
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.     IEnumerator JumpRoutine()
  35.     {
  36.         rb.velocity = Vector2.zero;
  37.         float timer = 0;
  38.        
  39.         while(Input.GetKey(KeyCode.Space) && timer < jumpTime)
  40.         {
  41.             //Add a constant force every frame of the jump
  42.             rb.AddForce(transform.up*jumpStr);
  43.             timer += Time.deltaTime;
  44.             yield return null;
  45.         }  
  46.         jumping = false;
  47.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement