Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- // remove this namespace bracketing
- namespace MAGD272Demo
- {
- public class jumpDemo : MonoBehaviour
- {
- // button to press for jumps
- [SerializeField] private KeyCode jumpButton;
- // the number of jumps made
- [SerializeField] private int numJumps;
- // the jump height value
- [SerializeField] private float jumpForce;
- // the gravity value when falling
- [SerializeField] private float fallingGrav;
- // whether or not we can jump
- [SerializeField] private bool canJump;
- // jump number limit
- [SerializeField] private int jumpLimit;
- [SerializeField] private bool onGround;
- // vertical value
- [SerializeField] private float verticalValue;
- // reference to our rigidbody
- private Rigidbody2D rb;
- private Animator animator;
- private void Awake()
- {
- // get reference of rigidbody on gameObject
- rb = GetComponent<Rigidbody2D>();
- animator = GetComponent<Animator>();
- }
- // Start is called before the first frame update
- void Start()
- {
- // reset number of jumps
- numJumps = 0;
- }
- // Update is called once per frame
- void Update()
- {
- GetJumpPress();
- Animate();
- }
- private void FixedUpdate()
- {
- // change our gravity scale when falling
- if (rb.velocity.y < 0)
- {
- rb.gravityScale = 1.2f;
- }
- else
- {
- rb.gravityScale = 1;
- }
- // ignores horizontal velocity, adds jumpHeight to vertical velocity
- rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y + verticalValue);
- // update the a
- }
- void Animate()
- {
- // send the animator our onGround State
- animator.SetBool("OnGround", onGround);
- // send the animator the rigidbody's vertical velocity
- animator.SetFloat("verticalValue", rb.velocity.y);
- }
- void GetJumpPress()
- {
- // check if user presses the jump button
- if (Input.GetKeyDown(jumpButton))
- {
- if (canJump || (numJumps < jumpLimit))
- {
- // setting our velocity.y to zero
- rb.velocity = new Vector2(rb.velocity.x, 0);
- rb.AddForce(new Vector2(0, jumpForce));
- numJumps++;
- }
- }
- }
- // detection of ground collider
- private void OnTriggerEnter2D(Collider2D collision)
- {
- // check if collider has 'Ground' tag
- if (collision.CompareTag("Ground"))
- {
- onGround = true;
- canJump = true;
- numJumps = 0;
- }
- }
- // detection of ground collider
- private void OnTriggerExit2D(Collider2D collision)
- {
- //check if collider has 'Ground' tag
- if (collision.CompareTag("Ground"))
- {
- Debug.Log("off ground");
- canJump = false;
- onGround = false;
- }
- }
- private void OnTriggerStay2D(Collider2D collision)
- {
- // check if collider has 'Ground' tag
- if (collision.CompareTag("Ground"))
- {
- Debug.Log("actually still on ground");
- canJump = true;
- numJumps = 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement