Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Platformer : MonoBehaviour
- {
- public Rigidbody2D rb;
- public float speed = 4;
- public float jumpForce;
- bool isGrounded = false;
- public Transform isGroundedChecker;
- public float checkGroundedRadius;
- public LayerMask groundLayer;
- public float fallMultiplier = 2.5f;
- public float lowJumpMultiplier = 2f;
- // Start is called before the first frame update
- void Start(){
- rb = GetComponent<Rigidbody2D>();
- }
- // Update is called once per frame
- void Update(){
- Move();
- Jump();
- CheckIfGrounded();
- BetterJump();
- }
- void Move() {
- float x = Input.GetAxisRaw("Horizontal");
- float moveBy = x * speed;
- rb.velocity = new Vector2(moveBy, rb.velocity.y);
- }
- void Jump(){
- if (Input.GetKeyDown(KeyCode.Space) && isGrounded) {
- rb.velocity = new Vector2(rb.velocity.x, jumpForce);
- }
- }
- void CheckIfGrounded() {
- Collider2D collider = Physics2D.OverlapCircle(isGroundedChecker.position, checkGroundedRadius, groundLayer);
- if (collider != null)
- {
- isGrounded = true;
- }else{
- isGrounded = false;
- }
- }
- void BetterJump() {
- if (rb.velocity.y < 0)
- {
- rb.velocity += Vector2.up * Physics2D.gravity * (fallMultiplier - 1) * Time.deltaTime;
- }else if(rb.velocity.y > 0 && !Input.GetKey(KeyCode.Space)){
- rb.velocity += Vector2.up * Physics2D.gravity * (lowJumpMultiplier - 1) * Time.deltaTime;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment