Advertisement
CakeMeister

2D platform extra Button controller mobile

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