Advertisement
CakeMeister

Player phan16

Jul 13th, 2017
809
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 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.     // Use this for initialization
  19.     void Start () {
  20.         r2 = gameObject.GetComponent<Rigidbody2D>();
  21.         anim = gameObject.GetComponent<Animator>();
  22.         gm = GameObject.FindGameObjectWithTag("gamemaster").GetComponent<gamemaster>();
  23.         ourHealth = maxhealth;
  24.         sound = GameObject.FindGameObjectWithTag("sound").GetComponent<SoundManager>();
  25. }
  26.    
  27.     // Update is called once per frame
  28.     void Update () {
  29.         anim.SetBool("Grounded", grounded);
  30.         anim.SetFloat("Speed", Mathf.Abs(r2.velocity.x));
  31.  
  32.         if (Input.GetKeyDown(KeyCode.Space))
  33.         {
  34.             if (grounded)
  35.             {
  36.                 grounded = false;
  37.                 doublejump = true;
  38.                 r2.AddForce(Vector2.up * jumpPow);
  39.  
  40.             }
  41.  
  42.             else
  43.             {
  44.                 if (doublejump)
  45.                 {
  46.                     doublejump = false;
  47.                     r2.velocity = new Vector2(r2.velocity.x, 0);
  48.                     r2.AddForce(Vector2.up * jumpPow * 0.7f);
  49.  
  50.                 }
  51.             }
  52.          
  53.  
  54.            
  55.  
  56.         }
  57.     }
  58.  
  59.     void FixedUpdate()
  60.     {
  61.         float h = Input.GetAxis("Horizontal");
  62.         r2.AddForce((Vector2.right) * speed * h);
  63.  
  64.         if (r2.velocity.x > maxspeed)
  65.             r2.velocity = new Vector2(maxspeed, r2.velocity.y);
  66.         if (r2.velocity.x < -maxspeed)
  67.             r2.velocity = new Vector2(-maxspeed, r2.velocity.y);
  68.  
  69.         if (r2.velocity.y > maxjump)
  70.             r2.velocity = new Vector2(r2.velocity.x, maxjump);
  71.         if (r2.velocity.y < -maxjump)
  72.             r2.velocity = new Vector2(r2.velocity.x, -maxjump);
  73.  
  74.  
  75.  
  76.         if (h>0 && !faceright)
  77.         {
  78.             Flip();
  79.         }
  80.  
  81.         if (h < 0 && faceright)
  82.         {
  83.             Flip();
  84.         }
  85.  
  86.         if (grounded)
  87.         {
  88.             r2.velocity = new Vector2(r2.velocity.x * 0.7f, r2.velocity.y);
  89.         }
  90.  
  91.         if (ourHealth <= 0)
  92.         {
  93.             Death();
  94.         }
  95.     }
  96.  
  97.     public void Flip()
  98.     {
  99.         faceright = !faceright;
  100.         Vector3 Scale;
  101.         Scale = transform.localScale;
  102.         Scale.x *= -1;
  103.         transform.localScale = Scale;
  104.     }
  105.  
  106.     public void Death()
  107.     {
  108.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
  109.  
  110.         if (PlayerPrefs.GetInt("highscore") < gm.points)
  111.             PlayerPrefs.SetInt("highscore", gm.points);
  112.     }
  113.    
  114.     public void Damage(int damage)
  115.     {
  116.         ourHealth -= damage;
  117.         gameObject.GetComponent<Animation>().Play("redflash");
  118.     }
  119.  
  120.     public void Knockback (float Knockpow, Vector2 Knockdir)
  121.     {
  122.         r2.velocity = new Vector2(0, 0);
  123.         r2.AddForce(new Vector2(Knockdir.x * -100, Knockdir.y * Knockpow));
  124.     }
  125.  
  126.     private void OnTriggerEnter2D(Collider2D col)
  127.     {
  128.         if (col.CompareTag("Coins"))
  129.         {
  130.             sound.Playsound("coins");
  131.             Destroy(col.gameObject);
  132.             gm.points += 1;
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement