Advertisement
Guest User

My Player Script

a guest
Aug 18th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.SceneManagement;
  4.  
  5. public class Player : MonoBehaviour {
  6.  
  7.     //Floats
  8.     public float maxSpeed = 3;
  9.     public float speed = 50f;
  10.     public float jumpPower = 200f;
  11.  
  12.     //Booleans
  13.     public bool grounded;
  14.     public bool canDoubleJump;
  15.     public bool facingRight = true;
  16.  
  17.     //Stats
  18.     public int curHealth;
  19.     public int maxHealth = 5;
  20.  
  21.     //References
  22.     private Rigidbody2D rb2d;
  23.     private Animator anim;
  24.     private GameMaster gm;
  25.  
  26.     void Start () {
  27.         rb2d = gameObject.GetComponent<Rigidbody2D> ();
  28.         anim = gameObject.GetComponent<Animator> ();
  29.  
  30.         curHealth = maxHealth;
  31.  
  32.         gm = GameObject.FindGameObjectWithTag ("GameMaster").GetComponent<GameMaster> ();
  33.     }
  34.  
  35.     void Update () {
  36.         anim.SetBool ("Grounded", grounded);
  37.         anim.SetFloat ("Speed", Mathf.Abs(rb2d.velocity.x));
  38.  
  39.         //Movement
  40.         if (Input.GetAxis ("Horizontal") < -0.1f) {
  41.             transform.localScale = new Vector3 (-1, 1, 1);
  42.             facingRight = false;
  43.         }
  44.         if (Input.GetAxis ("Horizontal") > 0.1f) {
  45.             transform.localScale = new Vector3 (1, 1, 1);
  46.             facingRight = true;
  47.         }
  48.         if (Input.GetButtonDown ("Jump")) {
  49.             if (grounded) {
  50.                 rb2d.AddForce (Vector2.up * jumpPower);
  51.                 canDoubleJump = true;
  52.             } else if (canDoubleJump) {
  53.                 canDoubleJump = false;
  54.                 rb2d.velocity = new Vector2 (rb2d.velocity.x, 0);
  55.                 rb2d.AddForce (Vector2.up * jumpPower);
  56.             }
  57.         }
  58.  
  59.         //Death
  60.         if (curHealth > maxHealth) {
  61.             curHealth = maxHealth;
  62.         } else if (curHealth <= 0) {
  63.             curHealth = 0;
  64.             Die ();
  65.         }
  66.     }
  67.  
  68.     void FixedUpdate() {
  69.         // Fake friction / easing the X speed of the player
  70.         Vector3 easeVelocity = rb2d.velocity;
  71.         easeVelocity.y = rb2d.velocity.y;
  72.         easeVelocity.z = 0.0f;
  73.         easeVelocity.x *= 0.75f;
  74.         if (grounded) {
  75.             rb2d.velocity = easeVelocity;
  76.         }
  77.  
  78.         // Moving the player
  79.         float h = Input.GetAxis ("Horizontal");
  80.         if (grounded) {
  81.             rb2d.AddForce ((Vector2.right * speed) * h);
  82.         } else {
  83.             rb2d.AddForce ((Vector2.right * speed / 2) * h);
  84.         }
  85.  
  86.         // Limit speed of the player
  87.         if (rb2d.velocity.x > maxSpeed) {
  88.             rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
  89.         }
  90.         if (rb2d.velocity.x < -maxSpeed) {
  91.             rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
  92.         }
  93.     }
  94.  
  95.     // All the functions that deal with getting hurt.
  96.     void Die() {
  97.         SceneManager.LoadScene (SceneManager.GetActiveScene().name);
  98.     }
  99.     public void Damage(int dmg) {
  100.         curHealth -= dmg;
  101.         gameObject.GetComponent<Animation> ().Play ("Hurt");
  102.     }
  103.     public IEnumerator Knockback(float knockDur, float knockPwr, Vector3 knockDir) {
  104.         float timer = 0;
  105.  
  106.         while (knockDur > timer) {
  107.             timer += Time.deltaTime;
  108.             rb2d.velocity = new Vector2 (0, 0);
  109.             rb2d.AddForce (new Vector3 (knockDir.x * -100, knockDir.y + knockPwr, transform.position.z)); // X: Opposite direction, Y: Knocked up depending on how powerful the knockback is, Z: No change.
  110.         }
  111.         yield return 0;
  112.     }
  113.  
  114.     // Handles picking up Coins and Hearts
  115.     void OnTriggerEnter2D(Collider2D col) {
  116.         if (col.CompareTag ("Coin")) {
  117.             Destroy (col.gameObject);
  118.             gm.score += 1;
  119.         } else if (col.CompareTag ("Heart") && curHealth < maxHealth) {
  120.             Destroy (col.gameObject);
  121.             curHealth += 1;
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement