Advertisement
Guest User

PlatformerCharacter2D.cs

a guest
Feb 20th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlatformerCharacter2D : MonoBehaviour
  4. {
  5.     bool facingRight = true;                            // For determining which way the player is currently facing.
  6.  
  7.     [SerializeField]
  8.     float maxSpeed = 10f;               // The fastest the player can travel in the x axis.
  9.     [SerializeField]
  10.     float jumpForce = 400f;         // Amount of force added when the player jumps.
  11.  
  12.     //[Range(0, 1)]
  13.     //[SerializeField]
  14.     //float crouchSpeed = .36f;         // Amount of maxSpeed applied to crouching movement. 1 = 100%
  15.  
  16.     [SerializeField]
  17.     bool airControl = false;            // Whether or not a player can steer while jumping;
  18.     [SerializeField]
  19.     LayerMask whatIsGround;         // A mask determining what is ground to the character
  20.  
  21.     Transform groundCheck;                              // A position marking where to check if the player is grounded.
  22.     float groundedRadius = .3f;                         // Radius of the overlap circle to determine if grounded
  23.     bool grounded = false;                              // Whether or not the player is grounded.
  24.     //Transform ceilingCheck;                               // A position marking where to check for ceilings
  25.     //float ceilingRadius = .01f;                           // Radius of the overlap circle to determine if the player can stand up
  26.     Animator anim;                                      // Reference to the player's animator component.
  27.  
  28.     bool doubleJump = false;
  29.  
  30.     void Awake()
  31.     {
  32.         // Setting up references.
  33.         groundCheck = transform.Find("GroundCheck");
  34.         //ceilingCheck = transform.Find("CeilingCheck");
  35.         anim = GetComponent<Animator>();
  36.     }
  37.  
  38.  
  39.     void FixedUpdate()
  40.     {
  41.         // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
  42.         grounded = Physics2D.OverlapCircle(groundCheck.position, groundedRadius, whatIsGround);
  43.         anim.SetBool("Ground", grounded);
  44.  
  45.         if (grounded)
  46.             doubleJump = false;
  47.  
  48.         // Set the vertical animation
  49.         anim.SetFloat("vSpeed", rigidbody2D.velocity.y);
  50.     }
  51.  
  52.  
  53.     public void Move(float move, bool crouch, bool jump)
  54.     {
  55.  
  56.  
  57.         //// If crouching, check to see if the character can stand up
  58.         //if (!crouch && anim.GetBool("Crouch"))
  59.         //{
  60.         //    // If the character has a ceiling preventing them from standing up, keep them crouching
  61.         //    if (Physics2D.OverlapCircle(ceilingCheck.position, ceilingRadius, whatIsGround))
  62.         //        crouch = true;
  63.         //}
  64.  
  65.         //// Set whether or not the character is crouching in the animator
  66.         //anim.SetBool("Crouch", crouch);
  67.  
  68.         //only control the player if grounded or airControl is turned on
  69.         if (grounded || airControl)
  70.         {
  71.             //// Reduce the speed if crouching by the crouchSpeed multiplier
  72.             //move = (crouch ? move * crouchSpeed : move);
  73.  
  74.             // The Speed animator parameter is set to the absolute value of the horizontal input.
  75.             anim.SetFloat("Speed", Mathf.Abs(move));
  76.  
  77.             // Move the character
  78.             rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
  79.  
  80.             // If the input is moving the player right and the player is facing left...
  81.             if (move > 0 && !facingRight)
  82.                 // ... flip the player.
  83.                 Flip();
  84.             // Otherwise if the input is moving the player left and the player is facing right...
  85.             else if (move < 0 && facingRight)
  86.                 // ... flip the player.
  87.                 Flip();
  88.         }
  89.  
  90.         // If the player should jump...
  91.         if ((grounded || !doubleJump) && jump)
  92.         {
  93.             // Add a vertical force to the player.
  94.             anim.SetBool("Ground", false);
  95.  
  96.             if (!grounded)
  97.                 doubleJump = true;
  98.             rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
  99.  
  100.             rigidbody2D.AddForce(new Vector2(0f, jumpForce));
  101.  
  102.         }
  103.     }
  104.  
  105.  
  106.     void Flip()
  107.     {
  108.         // Switch the way the player is labelled as facing.
  109.         facingRight = !facingRight;
  110.  
  111.         // Multiply the player's x local scale by -1.
  112.         Vector3 theScale = transform.localScale;
  113.         theScale.x *= -1;
  114.         transform.localScale = theScale;
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement