Advertisement
CakeMeister

player phan 15

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