This is the code I have written, don’t criticize the formatting or how “redundant” or “unreadable”
my code is as I simply do not care because it works for me.
{
// Components
private Rigidbody2D rb;
private Animator animator;
private SpriteRenderer spriteRenderer;
// Movement Settings
public float moveSpeed = 5f;
public float jumpForce = 10f;
private float horizontal;
public bool isFacingRight = true;
// Squish Settings
public float squishAmount = 100f; // Amount of squish on impact
public float squishDuration = 0.2f; // Duration of squish effect
// Strech Settings
public float stretchFactor = 120f; // How much to stretch vertically when falling
public float squishFactor = 80f; // How much to squish horizontally when falling
public float stretchSpeed = 10f; // Speed at which the stretch and squish effect is applied
// Dash Settings
public float dashForce = 20f; // Force applied during the dash
public float dashDuration = 0.2f; // How long the dash lasts
public float dashCooldown = 1f; // Cooldown before another dash can be performed
private bool isDashing = false;
private bool canDash = true;
private bool hasAirDashed = false; // Indicates if the player has dashed once in the air
float moveX;
// Impact Gravity Settings
public float impactGravity = 3f; // Increased gravity for impact effect
public float impactDuration = 2f; // Duration of increased gravity effect
// State Management
private bool isGrounded;
private bool wasGroundedLastFrame;
// Ground Check Settings
public Transform groundCheck;
public float groundCheckRadius = 20f;
public LayerMask groundLayer;
[SerializeField] private TrailRenderer tr;
public Transform wallCheck;
bool isWallTouch;
bool isSliding;
public float wallSlidingSpeed;
public float wallJumpDuration;
public Vector2 wallJumpForce;
bool isWallJumping;
private float previousYVelocity;
private float originalGravity;
void Start()
{
// Initialize components
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>(); // Assumes an Animator component is attached for animations
spriteRenderer = GetComponent<SpriteRenderer>();
originalGravity = rb.gravityScale;
}
void Update()
{
// Check if grounded
isGrounded = Physics2D.OverlapBox(groundCheck.position, new Vector2(0.8f, 0.2f), 0, groundLayer);
if (isGrounded && !wasGroundedLastFrame)
{
canDash = true;
hasAirDashed = false;
}
isWallTouch = Physics2D.OverlapBox(wallCheck.position, new Vector2(1.1f, 0.9f), -0.46f, groundLayer);
if (isWallTouch && !isGrounded && moveX != 0)
{
isSliding = true;
}
else
{
isSliding = false;
}
if (isGrounded)
{
animator.SetBool("isJumping", false);
}
if (!isGrounded)
{
animator.SetBool("isJumping", true);
}
if (isWallTouch)
{
animator.SetBool("isWallTouch", true);
}
if (!isWallTouch)
{
animator.SetBool("isWallTouch", false);
}
if (isDashing)
{
tr.emitting = true;
}
if (!isDashing)
{
tr.emitting = false;
}
if (isWallJumping)
{
rb.velocity = new Vector2(-moveX * wallJumpForce.x, wallJumpForce.y);
}
else
{
rb.velocity = new Vector2(moveX * moveSpeed, rb.velocity.y);
}
// Handle movement and jumping
HandleMovement();
HandleJump();
HandleDash();
HandleStretchEffect();
Flip();
// Check landing impact for squish effect
if (isGrounded && !wasGroundedLastFrame && !isWallTouch)
{
StartCoroutine(Squish());
}
// Store ground state for next frame comparison
wasGroundedLastFrame = isGrounded;
previousYVelocity = rb.velocity.y;
}
private void HandleMovement()
{
moveX = Input.GetAxis("Horizontal");
if (!isDashing)
{
if (moveX != 0)
{
// Apply movement
rb.velocity = new Vector2(moveX * moveSpeed, rb.velocity.y);
spriteRenderer.flipX = moveX < 0;
}
else
{
// Stop the player instantly when no input is detected
rb.velocity = new Vector2(0, rb.velocity.y);
}
}
}
private void HandleJump()
{
if (Input.GetButtonDown("Jump") && isGrounded)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce); // Set Y velocity to jump force
isGrounded = false;
animator.SetBool("isJumping", !isGrounded);
}
else if (isSliding && Input.GetButtonDown("Jump"))
{
isWallJumping = true;
Invoke("StopWallJump", wallJumpDuration);
}
if (Input.GetButtonUp("Jump") && rb.velocity.y > 0)
{
// Reduce the upward velocity by 50% when the jump button is released
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
}
}
void StopWallJump()
{
isWallJumping = false;
}
private void Flip()
{
if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
{
isFacingRight = !isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x *= -1f;
transform.localScale = localScale;
}
}
private void FixedUpdate()
{
animator.SetFloat("xVelocity", Mathf.Abs(rb.velocity.x));
animator.SetFloat("yVelocity", rb.velocity.y);
if (isSliding)
{
rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue));
}
}
private IEnumerator Squish()
{
// Record the original scale
Vector3 originalScale = transform.localScale;
// Calculate the squished scale
Vector3 squishedScale = new Vector3(originalScale.x + squishAmount, originalScale.y - squishAmount, originalScale.z);
// Apply the squished scale
transform.localScale = squishedScale;
// Wait for the duration of the squish effect
yield return new WaitForSeconds(squishDuration);
// Revert to the original scale
transform.localScale = originalScale;
}
private void HandleDash()
{
if (Input.GetButtonDown("Fire3") && canDash) // "Fire3" is typically bound to Left Shift or a controller button
{
StartCoroutine(Dash());
}
if (isDashing && !Input.GetButton("Fire3"))
{
StopDash();
}
}
private IEnumerator Dash()
{
isDashing = true;
if (!isGrounded)
{
canDash = false;
hasAirDashed = true;
}
rb.gravityScale = 0; // Temporarily set gravity to zero for the dash
Vector2 dashDirection = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
if (dashDirection == Vector2.zero)
{
dashDirection = spriteRenderer.flipX ? Vector2.left : Vector2.right; // Default dash direction based on facing
}
rb.velocity = dashDirection.normalized * dashForce;
// Keep dashing until the dash duration or until the button is released
float dashEndTime = Time.time + dashDuration;
while (Time.time < dashEndTime && Input.GetButton("Fire3"))
{
yield return null;
}
// Restore gravity and stop dashing
StopDash();
yield return new WaitForSeconds(dashCooldown);
if (isGrounded)
{
canDash = true;
}
}
private void StopDash()
{
rb.velocity = new Vector2(0, rb.velocity.y); // Stop horizontal movement
isDashing = false;
StartCoroutine(ImpactGravity());
}
private IEnumerator ImpactGravity()
{
// Temporarily increase gravity
rb.gravityScale = impactGravity;
// Wait for the duration of the impact effect
yield return new WaitForSeconds(impactDuration);
// Restore original gravity
rb.gravityScale = originalGravity;
isDashing = false;
}
private void HandleStretchEffect()
{
if (!isGrounded && !isWallTouch && rb.velocity.y < 0)
{
// Apply stretch effect while falling
Vector3 newScale = new Vector3(squishFactor, stretchFactor, 100f);
transform.localScale = Vector3.Lerp(transform.localScale, newScale, Time.deltaTime * stretchSpeed);
}
else
{
// Return to normal scale when not falling
transform.localScale = Vector3.Lerp(transform.localScale, Vector3.one, Time.deltaTime * stretchSpeed);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
animator.SetBool("isJumping", !isGrounded);
}
any help would be appreciated