Advertisement
CakeMeister

Player No-Redflash phan 9

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