Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class Player : MonoBehaviour {
- public float speed = 50f, maxspeed = 3, maxjump = 4, jumpPow = 220f;
- public bool grounded = true, faceright = true, doublejump = false;
- public int ourHealth;
- public int maxhealth = 5;
- public Rigidbody2D r2;
- public Animator anim;
- public gamemaster gm;
- public SoundManager sound;
- public float h = 0;
- public bool jump = false;
- // Use this for initialization
- void Start () {
- r2 = gameObject.GetComponent<Rigidbody2D>();
- anim = gameObject.GetComponent<Animator>();
- gm = GameObject.FindGameObjectWithTag("gamemaster").GetComponent<gamemaster>();
- ourHealth = maxhealth;
- sound = GameObject.FindGameObjectWithTag("sound").GetComponent<SoundManager>();
- }
- // Update is called once per frame
- void Update () {
- anim.SetBool("Grounded", grounded);
- anim.SetFloat("Speed", Mathf.Abs(r2.velocity.x));
- if (jump == true)
- {
- if (grounded)
- {
- grounded = false;
- doublejump = true;
- r2.AddForce(Vector2.up * jumpPow);
- }
- else
- {
- StartCoroutine(Djump());
- }
- }
- }
- public IEnumerator Djump()
- {
- yield return new WaitForSeconds(0.5f);
- if (doublejump && jump == true)
- {
- doublejump = false;
- r2.velocity = new Vector2(r2.velocity.x, 0);
- r2.AddForce(Vector2.up * jumpPow * 0.7f);
- }
- }
- public void Jumping(bool Bjump)
- {
- jump = Bjump;
- }
- public void Move(float Binput)
- {
- h = Binput;
- }
- void FixedUpdate()
- {
- Move(h);
- r2.AddForce((Vector2.right) * speed * h);
- if (r2.velocity.x > maxspeed)
- r2.velocity = new Vector2(maxspeed, r2.velocity.y);
- if (r2.velocity.x < -maxspeed)
- r2.velocity = new Vector2(-maxspeed, r2.velocity.y);
- if (r2.velocity.y > maxjump)
- r2.velocity = new Vector2(r2.velocity.x, maxjump);
- if (r2.velocity.y < -maxjump)
- r2.velocity = new Vector2(r2.velocity.x, -maxjump);
- if (h>0 && !faceright)
- {
- Flip();
- }
- if (h < 0 && faceright)
- {
- Flip();
- }
- if (grounded)
- {
- r2.velocity = new Vector2(r2.velocity.x * 0.7f, r2.velocity.y);
- }
- if (ourHealth <= 0)
- {
- Death();
- }
- }
- public void Flip()
- {
- faceright = !faceright;
- Vector3 Scale;
- Scale = transform.localScale;
- Scale.x *= -1;
- transform.localScale = Scale;
- }
- public void Death()
- {
- SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
- if (PlayerPrefs.GetInt("highscore") < gm.points)
- PlayerPrefs.SetInt("highscore", gm.points);
- }
- public void Damage(int damage)
- {
- ourHealth -= damage;
- gameObject.GetComponent<Animation>().Play("redflash");
- }
- public void Knockback (float Knockpow, Vector2 Knockdir)
- {
- r2.velocity = new Vector2(0, 0);
- r2.AddForce(new Vector2(Knockdir.x * -100, Knockdir.y * Knockpow));
- }
- private void OnTriggerEnter2D(Collider2D col)
- {
- if (col.CompareTag("Coins"))
- {
- sound.Playsound("coins");
- Destroy(col.gameObject);
- gm.points += 1;
- }
- if (col.CompareTag("shoe"))
- {
- Destroy(col.gameObject);
- maxspeed = 6f;
- speed = 100f;
- StartCoroutine(timecount(5));
- }
- if (col.CompareTag("heart"))
- {
- Destroy(col.gameObject);
- ourHealth = 5;
- }
- }
- IEnumerator timecount (float time)
- {
- yield return new WaitForSeconds(time);
- maxspeed = 3f;
- speed = 50f;
- yield return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement