Advertisement
Guest User

jump_demo

a guest
Apr 4th, 2020
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.75 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 Animator animator;
  32.  
  33.         private void Awake()
  34.         {
  35.             // get reference of rigidbody on gameObject
  36.             rb = GetComponent<Rigidbody2D>();
  37.             animator = GetComponent<Animator>();
  38.         }
  39.  
  40.  
  41.         // Start is called before the first frame update
  42.         void Start()
  43.         {
  44.             // reset number of jumps
  45.             numJumps = 0;
  46.         }
  47.  
  48.         // Update is called once per frame
  49.         void Update()
  50.         {
  51.             GetJumpPress();
  52.             Animate();
  53.         }
  54.  
  55.         private void FixedUpdate()
  56.         {
  57.             // change our gravity scale when falling
  58.             if (rb.velocity.y < 0)
  59.             {
  60.                 rb.gravityScale = 1.2f;
  61.             }
  62.             else
  63.             {
  64.                 rb.gravityScale = 1;
  65.             }
  66.  
  67.             // ignores horizontal velocity, adds jumpHeight to vertical velocity
  68.             rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y + verticalValue);
  69.  
  70.             // update the a
  71.  
  72.         }
  73.  
  74.         void Animate()
  75.         {
  76.             // send the animator our onGround State
  77.             animator.SetBool("OnGround", onGround);
  78.             // send the animator the rigidbody's vertical velocity
  79.             animator.SetFloat("verticalValue", rb.velocity.y);
  80.         }
  81.  
  82.         void GetJumpPress()
  83.         {
  84.             // check if user presses the jump button
  85.             if (Input.GetKeyDown(jumpButton))
  86.             {
  87.                 if (canJump || (numJumps < jumpLimit))
  88.                 {
  89.                     // setting our velocity.y to zero
  90.                     rb.velocity = new Vector2(rb.velocity.x, 0);
  91.                     rb.AddForce(new Vector2(0, jumpForce));
  92.                     numJumps++;
  93.                 }
  94.             }
  95.         }
  96.  
  97.         // detection of ground collider
  98.         private void OnTriggerEnter2D(Collider2D collision)
  99.         {
  100.             // check if collider has 'Ground' tag
  101.             if (collision.CompareTag("Ground"))
  102.             {
  103.                 onGround = true;
  104.                 canJump = true;
  105.                 numJumps = 0;
  106.             }
  107.         }
  108.  
  109.         // detection of ground collider
  110.         private void OnTriggerExit2D(Collider2D collision)
  111.         {
  112.              //check if collider has 'Ground' tag
  113.             if (collision.CompareTag("Ground"))
  114.             {
  115.                 Debug.Log("off ground");
  116.                 canJump = false;
  117.                 onGround = false;
  118.             }
  119.         }
  120.  
  121.         private void OnTriggerStay2D(Collider2D collision)
  122.         {
  123.             // check if collider has 'Ground' tag
  124.             if (collision.CompareTag("Ground"))
  125.             {
  126.                 Debug.Log("actually still on ground");
  127.                 canJump = true;
  128.                 numJumps = 0;
  129.             }
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement