Advertisement
Guest User

Untitled

a guest
Jun 6th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour {
  6.     private Rigidbody2D myRigidbody;
  7.  
  8.     private Animator myAnimator;
  9.  
  10.     [SerializeField]
  11.     private float movementSpeed;
  12.  
  13.     private bool facingRight;
  14.  
  15.     [SerializeField]
  16.     private bool airControl;
  17.  
  18.     [SerializeField]
  19.     private Transform[] groundPoints;
  20.  
  21.     [SerializeField]
  22.     private float groundRadius;
  23.  
  24.     [SerializeField]
  25.     private LayerMask whatIsGround;
  26.  
  27.     private bool grounded;
  28.  
  29.     private bool jump;
  30.  
  31.  
  32.  
  33.     [SerializeField]
  34.     private float jumpForce;
  35.  
  36.     // Use this for initialization
  37.     void Start () {
  38.         facingRight = true;
  39.         myRigidbody = GetComponent<Rigidbody2D> ();
  40.         myAnimator = GetComponent<Animator> ();
  41.     }
  42.    
  43.     // Update is called once per frame
  44.     void Update()
  45.     {
  46.         HandleInput ();
  47.     }
  48.  
  49.     private void FixedUpdate ()
  50.     {
  51.         float horizontal = Input.GetAxis("Horizontal");
  52.  
  53.         grounded = IsGrounded();
  54.  
  55.         HandleMovement(horizontal);
  56.  
  57.         Flip(horizontal);
  58.  
  59.         HandleLayers ();
  60.  
  61.         ResetValues ();
  62.        
  63.     }
  64.  
  65.     private void HandleMovement(float horizontal)
  66.     {
  67.         if (myRigidbody.velocity.y < 0)
  68.         {
  69.             myAnimator.SetBool ("Land", true);
  70.         }
  71.     if (grounded && jump)
  72.     {
  73.         grounded = false;
  74.  
  75.         myRigidbody.AddForce(new Vector2(0f,700));
  76.             myAnimator.SetTrigger ("Jump");
  77.     }
  78.         if(grounded || airControl)
  79.     {
  80.         myRigidbody.velocity = new Vector2 (horizontal * movementSpeed, myRigidbody.velocity.y);
  81.  
  82.         myAnimator.SetFloat ("speed", Mathf.Abs(horizontal));
  83.         }
  84.     }
  85.  
  86.     private void HandleInput()
  87.     {
  88.         if (Input.GetKeyDown(KeyCode.Space))
  89.         {jump = true;
  90.             }
  91.         }
  92.  
  93.  
  94.     private void Flip(float horizontal)
  95.     {
  96.         if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight) {
  97.             facingRight = !facingRight;
  98.  
  99.             Vector3 theScale = transform.localScale;
  100.  
  101.             theScale.x *= -1;
  102.  
  103.             transform.localScale = theScale;
  104.         }
  105.     }
  106.  
  107.         private void ResetValues()
  108.         {
  109.             jump = false;
  110. }
  111.     private bool IsGrounded()
  112.     {
  113.         //Cheks if we are falling or if the y axis is steady
  114.         if (myRigidbody.velocity.y <= 0)
  115.         {
  116.             //Runs through all the ground points
  117.             foreach (Transform point in groundPoints)
  118.             {
  119.                 //Makes an array of all colliders that are overlapping the ground points
  120.                 Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
  121.  
  122.                 //Runs through the colliders to check if we are overlapping something that isn't the player
  123.                 for (int i = 0; i < colliders.Length; i++)
  124.                 {
  125.                     if (colliders[i].gameObject != gameObject) //If we are overlapping something else than our self
  126.                     {
  127.                         myAnimator.ResetTrigger ("jump");
  128.                         myAnimator.SetBool ("Land", false);//Stops the land animation
  129.                         return true;
  130.                     }
  131.                 }
  132.             }
  133.         }
  134.  
  135.  
  136.         return false; //We are not grounded
  137.     }
  138.  
  139.     private void HandleLayers()
  140.     {
  141.         if(!grounded)
  142.         {
  143.             myAnimator.SetLayerWeight (1, 1);
  144.         }
  145.         else
  146.         {
  147.             myAnimator.SetLayerWeight (1, 0);
  148. }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement