Advertisement
CakeMeister

Player phan 19 extra

Jul 17th, 2017
619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.01 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5.  
  6. public class Player : MonoBehaviour {
  7.  
  8.     public float speed = 50f, maxspeed = 3, maxjump = 4, jumpPow = 220f;
  9.     public bool grounded = true, faceright = true, doublejump = false;
  10.  
  11.     public int ourHealth;
  12.     public int maxhealth = 5;
  13.  
  14.     public Rigidbody2D r2;
  15.     public Animator anim;
  16.     public gamemaster gm;
  17.     public SoundManager sound;
  18.  
  19.     // Use this for initialization
  20.     void Start () {
  21.  
  22.         r2 = gameObject.GetComponent<Rigidbody2D>();
  23.         anim = gameObject.GetComponent<Animator>();
  24.         gm = GameObject.FindGameObjectWithTag("gamemaster").GetComponent<gamemaster>();
  25.         ourHealth = maxhealth;
  26.         sound = GameObject.FindGameObjectWithTag("sound").GetComponent<SoundManager>();
  27.        
  28.     }
  29.    
  30.     // Update is called once per frame
  31.     void Update () {
  32.         anim.SetBool("Grounded", grounded);
  33.         anim.SetFloat("Speed", Mathf.Abs(r2.velocity.x));
  34.  
  35.         if (Input.GetKeyDown(KeyCode.Space))
  36.         {
  37.             if (grounded)
  38.             {
  39.                 grounded = false;
  40.                 doublejump = true;
  41.                 r2.AddForce(Vector2.up * jumpPow);
  42.  
  43.             }
  44.  
  45.             else
  46.             {
  47.                 if (doublejump)
  48.                 {
  49.                     doublejump = false;
  50.                     r2.velocity = new Vector2(r2.velocity.x, 0);
  51.                     r2.AddForce(Vector2.up * jumpPow * 0.7f);
  52.  
  53.                 }
  54.             }
  55.          
  56.  
  57.            
  58.  
  59.         }
  60.     }
  61.  
  62.     void FixedUpdate()
  63.     {
  64.         float h = Input.GetAxis("Horizontal");
  65.         r2.AddForce((Vector2.right) * speed * h);
  66.  
  67.         if (r2.velocity.x > maxspeed)
  68.             r2.velocity = new Vector2(maxspeed, r2.velocity.y);
  69.         if (r2.velocity.x < -maxspeed)
  70.             r2.velocity = new Vector2(-maxspeed, r2.velocity.y);
  71.  
  72.         if (r2.velocity.y > maxjump)
  73.             r2.velocity = new Vector2(r2.velocity.x, maxjump);
  74.         if (r2.velocity.y < -maxjump)
  75.             r2.velocity = new Vector2(r2.velocity.x, -maxjump);
  76.  
  77.  
  78.  
  79.         if (h>0 && !faceright)
  80.         {
  81.             Flip();
  82.            
  83.            
  84.         }
  85.  
  86.         if (h < 0 && faceright)
  87.         {
  88.             Flip();
  89.    
  90.         }
  91.  
  92.         if (grounded)
  93.         {
  94.             r2.velocity = new Vector2(r2.velocity.x * 0.7f, r2.velocity.y);
  95.         }
  96.  
  97.         if (ourHealth <= 0)
  98.         {
  99.             Death();
  100.         }
  101.     }
  102.  
  103.     public void Flip()
  104.     {
  105.         faceright = !faceright;
  106.         Vector3 Scale;
  107.         Scale = transform.localScale;
  108.         Scale.x *= -1;
  109.         transform.localScale = Scale;
  110.     }
  111.  
  112.     public void Death()
  113.     {
  114.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
  115.  
  116.         if (PlayerPrefs.GetInt("highscore") < gm.points)
  117.             PlayerPrefs.SetInt("highscore", gm.points);
  118.     }
  119.    
  120.     public void Damage(int damage)
  121.     {
  122.         ourHealth -= damage;
  123.         gameObject.GetComponent<Animation>().Play("redflash");
  124.     }
  125.  
  126.     public void Knockback (float Knockpow, Vector2 Knockdir)
  127.     {
  128.         r2.velocity = new Vector2(0, 0);
  129.         r2.AddForce(new Vector2(Knockdir.x * -100, Knockdir.y * Knockpow));
  130.     }
  131.  
  132.     private void OnTriggerEnter2D(Collider2D col)
  133.     {
  134.        
  135.         if (col.CompareTag("Coins"))
  136.         {
  137.             sound.Playsound("coins");
  138.             Destroy(col.gameObject);
  139.             gm.points += 1;
  140.         }
  141.  
  142.         if (col.CompareTag("shoe"))
  143.         {
  144.            
  145.             Destroy(col.gameObject);
  146.             maxspeed = 6f;
  147.             speed = 100f;
  148.             StartCoroutine(timecount(5));
  149.         }
  150.  
  151.         if (col.CompareTag("heart"))
  152.         {
  153.  
  154.             Destroy(col.gameObject);
  155.             ourHealth = 5;
  156.         }
  157.     }
  158.  
  159.  IEnumerator timecount (float time)
  160.     {
  161.         yield return new WaitForSeconds(time);
  162.         maxspeed = 3f;
  163.         speed = 50f;
  164.         yield return 0;
  165.     }
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement