0
\$\begingroup\$

I would like to be able to control the intensity of the wall jump of the character, enabling the character to jump in short bursts upwards a wall and in long button presses across the wall. I have a wall jump as it is but is pretty bare bones and would like to spice it up a bit. Basically on button hold the player should lose control of the character and on release control is back. That's something I have been thinking about. All input is appreciated. Please keep in mind that my coding experience spans about a month and am trying to improve my coding practices whenever possible. Thank you.

This is my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class PlayerController : MonoBehaviour
{

    public Animator animator;
    
    private float moveInput;
    [SerializeField] Rigidbody2D rb;
    [SerializeField] private float speed;
    [SerializeField] private float jumpForce;
    [SerializeField] private float maxFallVelocity = -20;
    

    private bool isGrounded;
    public Transform feetPos;
    [SerializeField] private float checkRadius;
    public LayerMask whatIsGround;

    private float jumpTimeCounter;
    [SerializeField] private float jumpTime;
    private bool isJumping;

    IEnumerator dashCoroutine;
    bool isDashing;
    bool canDash = true;
    float normalGravity;

    bool isTouchingFront;
    public Transform frontCheck;
    bool wallSliding;
    [SerializeField] private float wallSlidingSpeed;
    [SerializeField] private float xWallForce;
    [SerializeField] private float yWallForce;
    [SerializeField] private float wallJumpTime;
    private float wallJumpDirection;
    private bool wallJumping;

    private bool facingRight = true;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        normalGravity = rb.gravityScale;
    }

    void Update()
    {
        //Debug.Log("The velocity in the Y-axis is: " + rb.velocity.y);
        Debug.Log("The velocity in the X-axis is: " + rb.velocity.x);
        isGrounded = Physics2D.OverlapCircle(feetPos.position, .2f, whatIsGround);
        //Animation
        if (Input.GetButtonDown("Jump") && rb.velocity.y == 0)
            rb.AddForce(Vector2.up * 700f);

        if (Mathf.Abs(moveInput) > 0 && rb.velocity.y == 0)
            animator.SetBool("Running", true);
        else
            animator.SetBool("Running", false);

        if (rb.velocity.y == 0)
        {
            animator.SetBool("Jumping", false);
            animator.SetBool("Falling", false);
        }

        if (rb.velocity.y > 0.2)
        {
            animator.SetBool("Jumping", true);
        }

        if (rb.velocity.y < 0)
        {
            animator.SetBool("Jumping", false);
            animator.SetBool("Falling", true);
        }

        if (isDashing && moveInput > 0)
        {
            animator.SetBool("Dashing", true);
        }
        else if (isDashing && moveInput < 0)
        {
            animator.SetBool("Dashing", true);
        }
        else
        {
            animator.SetBool("Dashing", false);
        }
        // Jumping
        if (isGrounded == true && Input.GetButtonDown("Jump"))
        {
            isJumping = true;
            jumpTimeCounter = jumpTime;
            rb.velocity = Vector2.up * jumpForce;
        }
        if (Input.GetButton("Jump") && isJumping == true)
        {
            if (jumpTimeCounter > 0)
            {
                rb.velocity = Vector2.up * jumpForce;
                jumpTimeCounter -= Time.deltaTime;
            }
            else
            {
                isJumping = false;
            }
        }
        if (Input.GetButtonUp("Jump") && rb.velocity.y > 0)
        {
            isJumping = false;
            rb.velocity = new Vector2(rb.velocity.x, 0f);
        }
        // DASHING
        if (Input.GetButtonDown("Dash") && canDash == true)
        {
            if (dashCoroutine != null)
            {
                StopCoroutine(dashCoroutine);
            }
            dashCoroutine = Dash(.4f, .5f);
            StartCoroutine(dashCoroutine);
        }
        // Wall Jumping
        isTouchingFront = Physics2D.OverlapCircle(frontCheck.position, .2f, whatIsGround);
        if (isTouchingFront == true && isGrounded == false)  //&& moveInput != 0
        {
            wallSliding = true;
        }else
        {
            wallSliding = false;
        }
        if (wallSliding)
        {
            rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue));
        }
        if (Input.GetButtonDown("Jump") && wallSliding == true)
        {
            Debug.Log("Started wall jumping. Will stop in " + wallJumpTime);
            wallJumping = true;
            Invoke("SetWallJumpingToFalse", wallJumpTime);
        }
    }

    private void FixedUpdate()
    {
        moveInput = Input.GetAxisRaw("Horizontal");
        rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
        //Need to implement a way for the walljump to be cancelled when releasing the jump button similar to the Jump mechanic from the ground. 
        if (wallJumping == true)
        {
            if(facingRight == true)
            {
                wallJumpDirection = -1f;
            } else
            {
                wallJumpDirection = 1f;
            }            
            rb.velocity = new Vector2(xWallForce * wallJumpDirection, yWallForce);
            
        }
        //Flipping
        if (moveInput > 0 && facingRight == false)
        {
            Flipper();
        }else if (moveInput < 0 && facingRight == true)
        {
            Flipper();
        }
        ///DASHING
        if (isDashing && moveInput > 0)
        {
            rb.AddForce(new Vector2(1f * 130, 0), ForceMode2D.Impulse);
        }
        if (isDashing && moveInput < 0)
        {
            rb.AddForce(new Vector2(1f * -130, 0), ForceMode2D.Impulse);
        }
    }
    //DASHING SCIENCE FICTION 
    IEnumerator Dash(float dashDuration, float dashCooldown)
    {
        Vector2 originalVelocity = rb.velocity;
        isDashing = true;
        canDash = false;
        rb.gravityScale = 0;
        rb.velocity = new Vector2(originalVelocity.x, 0);
        yield return new WaitForSeconds(dashDuration);
        isDashing = false;
        rb.gravityScale = normalGravity;
        rb.velocity = new Vector2(originalVelocity.x, 0);
        yield return new WaitForSeconds(dashCooldown);
        canDash = true;
    }

    void Flipper()
    {
        transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
        facingRight = !facingRight;
    }

    void SetWallJumpingToFalse()
    {
        Debug.Log("Setting wallJumping to false");
        wallJumping = false;
    }    
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.