HenryKw

Untitled

Sep 19th, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Platformer : MonoBehaviour
  6. {
  7.     public Rigidbody2D rb;
  8.     public float speed = 4;
  9.     public float jumpForce;
  10.  
  11.     bool isGrounded = false;
  12.     public Transform isGroundedChecker;
  13.     public float checkGroundedRadius;
  14.     public LayerMask groundLayer;
  15.  
  16.     public float fallMultiplier = 2.5f;
  17.     public float lowJumpMultiplier = 2f;
  18.  
  19.     // Start is called before the first frame update
  20.     void Start(){
  21.         rb = GetComponent<Rigidbody2D>();
  22.     }
  23.  
  24.     // Update is called once per frame
  25.     void Update(){
  26.         Move();
  27.         Jump();
  28.         CheckIfGrounded();
  29.         BetterJump();
  30.     }
  31.  
  32.     void Move() {
  33.         float x = Input.GetAxisRaw("Horizontal");
  34.         float moveBy = x * speed;
  35.         rb.velocity = new Vector2(moveBy, rb.velocity.y);
  36.     }
  37.  
  38.     void Jump(){
  39.         if (Input.GetKeyDown(KeyCode.Space) && isGrounded) {
  40.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
  41.         }
  42.     }
  43.  
  44.     void CheckIfGrounded() {
  45.         Collider2D collider = Physics2D.OverlapCircle(isGroundedChecker.position, checkGroundedRadius, groundLayer);
  46.  
  47.         if (collider != null)
  48.         {
  49.             isGrounded = true;
  50.         }else{
  51.             isGrounded = false;
  52.         }
  53.     }
  54.  
  55.     void BetterJump() {
  56.         if (rb.velocity.y < 0)
  57.         {
  58.             rb.velocity += Vector2.up * Physics2D.gravity * (fallMultiplier - 1) * Time.deltaTime;
  59.         }else if(rb.velocity.y > 0 && !Input.GetKey(KeyCode.Space)){
  60.             rb.velocity += Vector2.up * Physics2D.gravity * (lowJumpMultiplier - 1) * Time.deltaTime;
  61.         }
  62.     }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment