Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.SceneManagement;
- public class Player : MonoBehaviour {
- //Floats
- public float maxSpeed = 3;
- public float speed = 50f;
- public float jumpPower = 200f;
- //Booleans
- public bool grounded;
- public bool canDoubleJump;
- public bool facingRight = true;
- //Stats
- public int curHealth;
- public int maxHealth = 5;
- //References
- private Rigidbody2D rb2d;
- private Animator anim;
- private GameMaster gm;
- void Start () {
- rb2d = gameObject.GetComponent<Rigidbody2D> ();
- anim = gameObject.GetComponent<Animator> ();
- curHealth = maxHealth;
- gm = GameObject.FindGameObjectWithTag ("GameMaster").GetComponent<GameMaster> ();
- }
- void Update () {
- anim.SetBool ("Grounded", grounded);
- anim.SetFloat ("Speed", Mathf.Abs(rb2d.velocity.x));
- //Movement
- if (Input.GetAxis ("Horizontal") < -0.1f) {
- transform.localScale = new Vector3 (-1, 1, 1);
- facingRight = false;
- }
- if (Input.GetAxis ("Horizontal") > 0.1f) {
- transform.localScale = new Vector3 (1, 1, 1);
- facingRight = true;
- }
- if (Input.GetButtonDown ("Jump")) {
- if (grounded) {
- rb2d.AddForce (Vector2.up * jumpPower);
- canDoubleJump = true;
- } else if (canDoubleJump) {
- canDoubleJump = false;
- rb2d.velocity = new Vector2 (rb2d.velocity.x, 0);
- rb2d.AddForce (Vector2.up * jumpPower);
- }
- }
- //Death
- if (curHealth > maxHealth) {
- curHealth = maxHealth;
- } else if (curHealth <= 0) {
- curHealth = 0;
- Die ();
- }
- }
- void FixedUpdate() {
- // Fake friction / easing the X speed of the player
- Vector3 easeVelocity = rb2d.velocity;
- easeVelocity.y = rb2d.velocity.y;
- easeVelocity.z = 0.0f;
- easeVelocity.x *= 0.75f;
- if (grounded) {
- rb2d.velocity = easeVelocity;
- }
- // Moving the player
- float h = Input.GetAxis ("Horizontal");
- if (grounded) {
- rb2d.AddForce ((Vector2.right * speed) * h);
- } else {
- rb2d.AddForce ((Vector2.right * speed / 2) * h);
- }
- // Limit speed of the player
- if (rb2d.velocity.x > maxSpeed) {
- rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
- }
- if (rb2d.velocity.x < -maxSpeed) {
- rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
- }
- }
- // All the functions that deal with getting hurt.
- void Die() {
- SceneManager.LoadScene (SceneManager.GetActiveScene().name);
- }
- public void Damage(int dmg) {
- curHealth -= dmg;
- gameObject.GetComponent<Animation> ().Play ("Hurt");
- }
- public IEnumerator Knockback(float knockDur, float knockPwr, Vector3 knockDir) {
- float timer = 0;
- while (knockDur > timer) {
- timer += Time.deltaTime;
- rb2d.velocity = new Vector2 (0, 0);
- 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.
- }
- yield return 0;
- }
- // Handles picking up Coins and Hearts
- void OnTriggerEnter2D(Collider2D col) {
- if (col.CompareTag ("Coin")) {
- Destroy (col.gameObject);
- gm.score += 1;
- } else if (col.CompareTag ("Heart") && curHealth < maxHealth) {
- Destroy (col.gameObject);
- curHealth += 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement