Advertisement
Guest User

Unity 2D ground check and jump

a guest
Apr 3rd, 2020
1,112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace MAGD272Demo
  6. {
  7.     public class jumpDemo : MonoBehaviour
  8.     {
  9.         // button to press for jumps
  10.         [SerializeField] private KeyCode jumpButton;
  11.         // the number of jumps made
  12.         [SerializeField] private int numJumps;
  13.         // the jump height value
  14.         [SerializeField] private float jumpForce;
  15.         // the gravity value when falling
  16.         [SerializeField] private float fallingGrav;
  17.         // whether or not we can jump
  18.         [SerializeField] private bool canJump;
  19.         // jump number limit
  20.         [SerializeField] private int jumpLimit;
  21.  
  22.         [SerializeField] private bool onGround;
  23.  
  24.         // vertical value
  25.         [SerializeField] private float verticalValue;
  26.  
  27.         // reference to our rigidbody
  28.         private Rigidbody2D rb;
  29.  
  30.         private void Awake()
  31.         {
  32.             // get reference of rigidbody on gameObject
  33.             rb = GetComponent<Rigidbody2D>();
  34.         }
  35.  
  36.  
  37.         // Start is called before the first frame update
  38.         void Start()
  39.         {
  40.             // reset number of jumps
  41.             numJumps = 0;
  42.         }
  43.  
  44.         // Update is called once per frame
  45.         void Update()
  46.         {
  47.             GetJumpPress();
  48.         }
  49.  
  50.         private void FixedUpdate()
  51.         {
  52.             // change our gravity scale when falling
  53.             if (rb.velocity.y < 0)
  54.             {
  55.                 rb.gravityScale = 1.2f;
  56.             }
  57.             else
  58.             {
  59.                 rb.gravityScale = 1;
  60.             }
  61.  
  62.             // ignores horizontal velocity, adds jumpHeight to vertical velocity
  63.             rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y + verticalValue);
  64.         }
  65.  
  66.         void GetJumpPress()
  67.         {
  68.             // check if user presses the jump button
  69.             if (Input.GetKeyDown(jumpButton))
  70.             {
  71.                 if (canJump || (numJumps < jumpLimit))
  72.                 {
  73.                     // setting our velocity.y to zero
  74.                     rb.velocity = new Vector2(rb.velocity.x, 0);
  75.                     rb.AddForce(new Vector2(0, jumpForce));
  76.                     numJumps++;
  77.                 }
  78.             }
  79.         }
  80.  
  81.         // detection of ground collider
  82.         private void OnTriggerEnter2D(Collider2D collision)
  83.         {
  84.             // check if collider has 'Ground' tag
  85.             if (collision.CompareTag("Ground"))
  86.             {
  87.                 onGround = true;
  88.                 canJump = true;
  89.                 numJumps = 0;
  90.             }
  91.         }
  92.  
  93.         // detection of ground collider
  94.         private void OnTriggerExit2D(Collider2D collision)
  95.         {
  96.              //check if collider has 'Ground' tag
  97.             if (collision.CompareTag("Ground"))
  98.             {
  99.                 Debug.Log("off ground");
  100.                 canJump = false;
  101.                 onGround = false;
  102.             }
  103.         }
  104.  
  105.         private void OnTriggerStay2D(Collider2D collision)
  106.         {
  107.             // check if collider has 'Ground' tag
  108.             if (collision.CompareTag("Ground"))
  109.             {
  110.                 Debug.Log("actually still on ground");
  111.                 canJump = true;
  112.                 numJumps = 0;
  113.             }
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement