Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Move : MonoBehaviour {
  6.  
  7.     public float speed = 5.0f;
  8.     public bool onGround;
  9.     public LayerMask ground;
  10.     bool isPowerup = false;
  11.  
  12.     Rigidbody2D rb2d;
  13.     SpriteRenderer sprite;
  14.     Animator anim;
  15.     Vector2 position;
  16.  
  17.     void Start(){
  18.         rb2d = GetComponent<Rigidbody2D>();
  19.         sprite = GetComponent<SpriteRenderer>();
  20.         anim = GetComponent<Animator>();
  21.     }
  22.  
  23.     void FixedUpdate(){
  24.         Movement();
  25.     }
  26.  
  27.     public bool onGrounded() {
  28.         if (!isPowerup)
  29.             position = transform.position;
  30.         else
  31.             position = transform.position - new Vector3(0,0.5f,0);
  32.         Vector2 direction = Vector2.down;
  33.         float distance = 1f;
  34.        
  35.         RaycastHit2D hit = Physics2D.Raycast(position, direction, distance, ground);
  36.         if (hit.collider != null) {
  37.             return true;
  38.         }
  39.         return false;
  40.     }
  41.  
  42.     void Movement(){
  43.         float hor = Input.GetAxisRaw("Horizontal");
  44.         rb2d.velocity = new Vector2((hor*speed), rb2d.velocity.y);
  45.  
  46.         if (hor<0)
  47.         {
  48.             if (onGrounded()){
  49.                 anim.SetBool("isMove", true);
  50.                 sprite.flipX = true;
  51.             }
  52.         } else if(hor>0) {
  53.             if (onGrounded()){
  54.                 anim.SetBool("isMove", true);
  55.                 sprite.flipX = false;
  56.             }
  57.         } else anim.SetBool("isMove", false);
  58.     }
  59.  
  60.     void OnCollisionEnter2D(Collision2D col){      
  61.         if(col.gameObject.tag == "Mushroom"){
  62.             isPowerup = true;
  63.             Destroy(col.gameObject);
  64.             anim.Play("powerup");
  65.             transform.position += new Vector3(0,1,0);
  66.             GetComponent<CapsuleCollider2D>().size = new Vector2(0.7f,2);
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement