This Unity Jumping & Gravity script is a mess, how can I fix it?

23 Views Asked by At

This is my code:

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor.Build;
using UnityEngine;

public class PlayerJump : MonoBehaviour
{
    [Header("Jump Properties")]
    [SerializeField] private Transform groundCheck;
    [SerializeField] private float jumpForce;
    [SerializeField] private float jumpTime;
    [SerializeField] private float jumpMultiplier;
    [SerializeField] private float fallMultiplier;
    [SerializeField] private LayerMask groundLayer;
    [SerializeField] private float groundCheckRadius = .02f;
    [SerializeField] private float mGroundedTimer;
    [SerializeField] private float mSpaceRemember;

    private bool grounded;
    public float cGroundedTimer;
    private float cSpaceRemember;
    private float jumpCounter;
    private bool isJumping;
    private Vector2 vecGravity;
    private PlayerMovement playerMovement;
    public Rigidbody2D rb; 

    private Vector2 boxSize = new Vector2(0.6f, 0.2f);

    // Start is called before the first frame update
    void Start()
    {
        vecGravity = new Vector2(0, -Physics2D.gravity.y);
        grounded = Physics2D.OverlapBox(groundCheck.position, boxSize, 0, groundLayer);

    }

    // Update is called once per frame
    void Update()
    {
        
        

        ResetTimers();

        if (cGroundedTimer > 0 && cSpaceRemember > 0)
        {
            Jump();
        }

        if (rb.velocity.y > 0 && isJumping)
        {
            jumpCounter += Time.deltaTime;
            if (jumpCounter > jumpTime) isJumping = false;


            rb.velocity += vecGravity * jumpMultiplier * Time.deltaTime;
        }

        if (Input.GetButtonUp("Jump") && isJumping)
        {
            isJumping = false;
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y / 2);
        }


        cGroundedTimer -= Time.deltaTime;
        cSpaceRemember -= Time.deltaTime;

    }

    void FixedUpdate()
    {
        if (rb.velocity.y > 0 && isJumping)
        {
            jumpCounter += Time.deltaTime;
            
            if (jumpCounter > jumpTime)
            {
                isJumping = false;
                Debug.Log("Penis");
            }


            rb.velocity += vecGravity * jumpMultiplier * Time.deltaTime;
        }

        if (rb.velocity.y < 0)
        {
            rb.velocity -= vecGravity * fallMultiplier * Time.deltaTime;

        }

        

        
    }

    void ResetTimers()
    {
        if (grounded)
        {
            cGroundedTimer = mGroundedTimer;
        }

        if (Input.GetButtonDown("Jump"))
        {
            cSpaceRemember = mSpaceRemember;
        }
    }

    void Jump()
    {
        rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        cGroundedTimer = 0;
        cSpaceRemember = 0;
        isJumping = true;

        jumpCounter = 0;
    }

    void OnDrawGizmos()
    {
        // Define the size of the box based on your ground check size
        Vector2 boxSize = new Vector2(0.7f, 0.2f);

       
        // Set the Gizmo color to green if grounded, red if not
        Gizmos.color = grounded ? Color.green : Color.red;

        // Draw a wire cube at the ground check's position with the specified box size
        Gizmos.DrawWireCube(groundCheck.position, new Vector3(boxSize.x, boxSize.y, 0));
    }


}

Do you have any idea how to fix this and make it less spaghetti? If I try to break it up into seperat functions it just stops working.

#############################################################################################################################################################################################################################################################################################

0

There are 0 best solutions below