toko214

PLAYER

Mar 22nd, 2017
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class main : MonoBehaviour
  6. {
  7.  
  8.     // Use this for initialization
  9.     Rigidbody2D playerRg;
  10.     BoxCollider2D col;
  11.     public BoxCollider2D floor;
  12.     Animator anim;
  13.     public int count;
  14.     public float playerSpeed;
  15.     public float movex;
  16.     public bool facingLeft;
  17.  
  18.     void Start()
  19.     {
  20.         playerRg = GetComponent<Rigidbody2D>();
  21.         col = GetComponent<BoxCollider2D>();
  22.         anim = GetComponent<Animator>();
  23.         count = 1;
  24.         playerSpeed = 4;
  25.         facingLeft = true;
  26.     }
  27.  
  28.     // Update is called once per frame
  29.     void Update()
  30.     {
  31.         float vy = playerRg.velocity.y;
  32.         float vx = playerRg.velocity.x;
  33.         movex = Input.GetAxis("Horizontal");
  34.         bool pressedJump = Input.GetKey(KeyCode.UpArrow);
  35.         bool isTouchingFloor = playerRg.IsTouchingLayers(LayerMask.GetMask("Floor"));
  36.         anim.SetFloat("speed", Mathf.Abs(movex));
  37.         //ANIMATIONS
  38.         if (movex > 0 && facingLeft)
  39.         {
  40.             Flip();
  41.             facingLeft = false;
  42.         }
  43.         if (movex < 0 && !facingLeft)
  44.         {
  45.             Flip();
  46.             facingLeft = true;
  47.         }
  48.         //HORIZONTAL MOVEMENT
  49.         if (isTouchingFloor && movex != 0)
  50.         {
  51.             playerRg.velocity = new Vector2(movex * playerSpeed, vy);
  52.         }
  53.  
  54.         //JUMP
  55.         if (pressedJump && count > 0)
  56.         {
  57.             playerRg.velocity = new Vector2(vx, 6);
  58.             count--;
  59.         }
  60.         //IF NOT PRESSING ANHYTHING AND ON THE FLOOR STOP THE PLAYER
  61.         if (isTouchingFloor && movex == 0 && !(pressedJump) && count == 1)
  62.         {
  63.             playerRg.velocity = new Vector2(0, 0);
  64.         }
  65.     }
  66.     //FLIP THE PLAYER BODY SPRITE
  67.     void Flip()
  68.     {
  69.         facingLeft = !facingLeft;
  70.         Vector3 theScale = transform.localScale;
  71.         theScale.x *= -1;
  72.         transform.localScale = theScale;
  73.     }
  74.  
  75.  
  76.     //RESET THE JUMP PARAMETER
  77.     void OnCollisionEnter2D(Collision2D coll)
  78.     {
  79.         if (coll.gameObject.tag == "Floor")
  80.         {
  81.             if (playerRg.transform.position.y >= coll.collider.transform.position.y)
  82.             {
  83.                 Debug.Log("asd");
  84.                 count = 1;
  85.             }
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment