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 PlayerMovement : MonoBehaviour
- {
- public float speed;
- public float jump;
- private bool IsGrounded = true;
- private float move;
- private Rigidbody2D rb;
- private bool facingRight;
- // Start is called before the first frame update
- void Start()
- {
- facingRight = true;
- rb = GetComponent<Rigidbody2D>();
- }
- // Update is called once per frame
- void FixedUpdate()
- {
- //IsGrounded
- if (IsGrounded == true)
- {
- if (Input.GetButtonDown("Jump"))
- {
- rb.AddForce(new Vector2(rb.velocity.x, jump));
- IsGrounded = false;
- }
- }
- move = Input.GetAxisRaw("Horizontal");
- rb.velocity = new Vector2(move * speed, rb.velocity.y);
- Flip("horizontal");
- }
- private void OnCollisionEnter2D(Collision2D collision)
- {
- if (collision.gameObject.tag == "Floor") { IsGrounded = true; }
- }
- private void Flip(float horizontal)
- {
- if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
- {
- facingRight = !facingRight;
- Vector3 theScale = transform.localScale;
- theScale.x *= -5;
- transform.localScale = theScale;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement