Advertisement
CakeMeister

Player script phan 8

Jun 30th, 2017
1,404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 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, 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 (h>0 && !faceright)
  67.         {
  68.             Flip();
  69.         }
  70.  
  71.         if (h < 0 && faceright)
  72.         {
  73.             Flip();
  74.         }
  75.  
  76.         if (grounded)
  77.         {
  78.             r2.velocity = new Vector2(r2.velocity.x * 0.7f, r2.velocity.y);
  79.         }
  80.  
  81.         if (ourHealth <= 0)
  82.         {
  83.             Death();
  84.         }
  85.     }
  86.  
  87.     public void Flip()
  88.     {
  89.         faceright = !faceright;
  90.         Vector3 Scale;
  91.         Scale = transform.localScale;
  92.         Scale.x *= -1;
  93.         transform.localScale = Scale;
  94.     }
  95.  
  96.     public void Death()
  97.     {
  98.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
  99.     }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement