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 main : MonoBehaviour
- {
- // Use this for initialization
- Rigidbody2D playerRg;
- BoxCollider2D col;
- public BoxCollider2D floor;
- Animator anim;
- public int count;
- public float playerSpeed;
- public float movex;
- public bool facingLeft;
- bool flooring;
- void Start()
- {
- playerRg = GetComponent<Rigidbody2D>();
- col = GetComponent<BoxCollider2D>();
- anim = GetComponent<Animator>();
- count = 1;
- playerSpeed = 4;
- facingLeft = true;
- }
- // Update is called once per frame
- void FixedUpdate()
- {
- float vy = playerRg.velocity.y;
- float vx = playerRg.velocity.x;
- movex = Input.GetAxis("Horizontal");
- bool pressedJump = Input.GetKey(KeyCode.UpArrow);
- bool isTouchingFloor = playerRg.IsTouchingLayers(LayerMask.GetMask("Floor"));
- anim.SetFloat("speed", Mathf.Abs(movex));
- //ANIMATIONS
- if (movex > 0 && facingLeft)
- {
- Flip();
- facingLeft = false;
- }
- if (movex < 0 && !facingLeft)
- {
- Flip();
- facingLeft = true;
- }
- //HORIZONTAL MOVEMENT
- if (isTouchingFloor && movex != 0)
- {
- playerRg.velocity = new Vector2(movex * playerSpeed, vy);
- }
- //JUMP
- if (pressedJump && count > 0)
- {
- playerRg.velocity = new Vector2(vx, 6);
- //playerRg.AddForce(transform.up * 60);
- count--;
- }
- //IF NOT PRESSING ANHYTHING AND ON THE FLOOR STOP THE PLAYER
- if (flooring && movex == 0 && !(pressedJump))
- {
- playerRg.velocity = new Vector2(0, 0);
- }
- }
- //FLIP THE PLAYER BODY SPRITE
- void Flip()
- {
- facingLeft = !facingLeft;
- Vector3 theScale = transform.localScale;
- theScale.x *= -1;
- transform.localScale = theScale;
- }
- //RESET THE JUMP PARAMETER
- void OnCollisionEnter2D(Collision2D coll)
- {
- if (coll.gameObject.tag == "Floor")
- {
- if (playerRg.transform.position.y >= coll.collider.transform.position.y)
- {
- Debug.Log("asd");
- count = 1;
- flooring = true;
- }
- }
- }
- void OnCollisionExit2D(Collision2D coll)
- {
- if (coll.gameObject.tag == "Floor")
- {
- flooring = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement