1. #pragma strict
  2.  
  3. //Public Variables
  4. var moveSpeed : float = 25;             // Movement Speed
  5. var jumpForce : float = 300;            // Jump Force
  6. var allowDoubleJump : boolean = true;   // Allow Double Jump
  7.  
  8. //Private Variables
  9. private var horizontalInput : float;
  10. private var verticalInput : float;
  11.  
  12. private var isFacingRight : boolean = true;
  13. public var isGrounded : boolean = false;
  14. private var isMoving : boolean = false;
  15. private var jumpCount = 0;
  16.  
  17. private var velX : float;
  18. private var animator : Animator;
  19.  
  20. function Update () {
  21.     animator = GetComponent("Animator");    // Get the "Animator" component and set it to "animator" var
  22.     velX = rigidbody2D.velocity.x;          // Store the x velocity in "vel" var
  23.        
  24.     HandleInput();          // Handles Input
  25.     HandleMovement();       // Handles Movement
  26.     HandleJump();
  27.     SetIsGrounded();        // Sets "isGrounded"
  28.     SetIsFacingRight();     // Sets "isFacingRight"
  29.     SetIsMoving();          // Sets "isMoving"
  30. }
  31.  
  32. function HandleInput() {
  33.     horizontalInput = Input.GetAxis("Horizontal");      // Set "horiztonalInput" equal to the Horizontal Axis Input
  34.     verticalInput = Input.GetAxis("Vertical");          // Set "verticallInput" equal to the Vertical Axis Inpu
  35. }
  36.  
  37. function HandleMovement() {
  38.     rigidbody2D.velocity.x = horizontalInput * moveSpeed * Time.deltaTime; // Moves gameObject based on the "moveSpeed" var
  39. }
  40.  
  41. function HandleJump() {
  42.     if(Input.GetButtonDown("Jump")) {                                       // When "Jump" button is pressed
  43.         jumpCount++;                                                        // Add 1 to jumpCount
  44.         if(isGrounded || (allowDoubleJump && jumpCount < 2)) {              // if the character is on the ground OR if double jump is allow and the "jumpCount" is below 2
  45.             rigidbody2D.velocity.y = 0;                                     // Set the y velocity to 0
  46.             rigidbody2D.AddForce(Vector2.up * jumpForce * Time.deltaTime);  // Add y force set by "jumpForce"
  47.         }
  48.     }
  49. }
  50.  
  51. function Flip() {
  52.     isFacingRight = !isFacingRight;                 // Toggles between "true" and "false"
  53.     gameObject.transform.localScale.x *= -1;        // Flip the gameObject based on localScale
  54. }
  55.  
  56. function SetIsFacingRight() {  
  57.     if(velX > 0 && !isFacingRight) {            // If velocity is positive and gameObject isn't facing right
  58.         Flip();
  59.     } else if(velX < 0 && isFacingRight) {      // If velocity is negative and gameObject is facing right
  60.         Flip();
  61.     }
  62. }
  63.  
  64. function SetIsMoving() {
  65.     if(velX != 0) {                                 // If velocity isn't 0, set "isMoving" to true
  66.         isMoving = true;   
  67.     } else {                                    // If velocity is 0, set "isMoving" to false
  68.         isMoving = false;
  69.     }
  70.     animator.SetBool("isMoving", isMoving);     // Set the "isMoving" bool parameter to equal "isMoving" var   
  71. }
  72.  
  73. function SetIsGrounded() {
  74.     var raycastHit : RaycastHit2D = Physics2D.Raycast(transform.position, -Vector2.up, 0.6);    // Create a ray from the character to check for the ground
  75.     if(raycastHit.collider != null) {                                                           // If the ray hit isn't null
  76.         if(raycastHit.collider.tag == "ground") {                                               // Check if the collider is in the "ground" tag
  77.             isGrounded = true;                                                                  // Set "isGrounded" to true
  78.             jumpCount = 0;                                                                      // Reset the "jumpCount" to 0
  79.         }
  80.     } else {
  81.         isGrounded = false;                                                                     // Set "isGrounded" to false if not colliding with "ground" tag
  82.     }
  83. }