Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Move : MonoBehaviour {
- public float speed = 5.0f;
- public bool onGround;
- public LayerMask ground;
- bool isPowerup = false;
- Rigidbody2D rb2d;
- SpriteRenderer sprite;
- Animator anim;
- Vector2 position;
- void Start(){
- rb2d = GetComponent<Rigidbody2D>();
- sprite = GetComponent<SpriteRenderer>();
- anim = GetComponent<Animator>();
- }
- void FixedUpdate(){
- Movement();
- }
- public bool onGrounded() {
- if (!isPowerup)
- position = transform.position;
- else
- position = transform.position - new Vector3(0,0.5f,0);
- Vector2 direction = Vector2.down;
- float distance = 1f;
- RaycastHit2D hit = Physics2D.Raycast(position, direction, distance, ground);
- if (hit.collider != null) {
- return true;
- }
- return false;
- }
- void Movement(){
- float hor = Input.GetAxisRaw("Horizontal");
- rb2d.velocity = new Vector2((hor*speed), rb2d.velocity.y);
- if (hor<0)
- {
- if (onGrounded()){
- anim.SetBool("isMove", true);
- sprite.flipX = true;
- }
- } else if(hor>0) {
- if (onGrounded()){
- anim.SetBool("isMove", true);
- sprite.flipX = false;
- }
- } else anim.SetBool("isMove", false);
- }
- void OnCollisionEnter2D(Collision2D col){
- if(col.gameObject.tag == "Mushroom"){
- isPowerup = true;
- Destroy(col.gameObject);
- anim.Play("powerup");
- transform.position += new Vector3(0,1,0);
- GetComponent<CapsuleCollider2D>().size = new Vector2(0.7f,2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement