Advertisement
Guest User

Untitled

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