Hello, I’m quite new to Unity and game development and I’m making a little platformer prototype to get started and learn. I have a problem however, and that is that when I walljump and let go of the directional button (A/D) my walljump continues in the Y-axis but not in the X-axis.
I think I know what is causing the problem but I don’t know how to fix it. Please help!
The causation of the problem seems to be that my ‘horizontal’ (horizontal input) becomes 0 if you let go of the directional input, therefore if you multiply the xWallForce by ‘-horizontal’ which is zero, nothing happens on the X-axis. Here’s what i mean using a bit of my ‘wallJumping’ code:
if (wallJumping == true)
{
rb.velocity = new Vector2(xWallForce * -horizontal, yWallForce);
}
How can I make it so that the X-axis movement from the walljump continues until ‘wallJumpTime’ is over, independent from whether I let go of my directional input or not.
Full code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public ParticleSystem dust;
public ParticleSystem dashExplo;
public float horizontal;
public float speed = 8f;
public float jumpingPower = 16f;
public float fallGravityMultiplier = 1.5f;
public bool isFacingRight = true;
public bool airborn;
private bool canDash = true;
private bool isDashing;
public float dashingPower = 40f;
public float dashingTime = 0.2f;
public float dashingCoooldown = 1f;
bool isTouchingFront;
public Transform frontCheck;
bool wallSliding;
public float wallSlidingSpeed;
public bool wallJumping;
public float xWallForce;
public float yWallForce;
public float wallJumpTime;
public float jumpTime;
private float jumpBufferTime = 0.2f;
private float jumpBufferCounter;
public float coyoteTime = 0.2f;
private float coyoteTimeCounter;
[SerializeField] private Rigidbody2D rb;
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;
[SerializeField] private TrailRenderer tr;
public float gravityScale;
public void SetGravityScale(float scale)
{
rb.gravityScale = scale;
}
private void Start()
{
SetGravityScale(gravityScale);
isFacingRight = true;
}
private void Update()
{
if (isDashing)
{
return;
}
horizontal = Input.GetAxisRaw("Horizontal");
if (isGrounded() == true)
{
coyoteTimeCounter = coyoteTime;
}
else
{
coyoteTimeCounter -= Time.deltaTime;
}
if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.Space))
{
jumpBufferCounter = jumpBufferTime;
}
else
{
jumpBufferCounter -= Time.deltaTime;
}
if (jumpBufferCounter > 0f && coyoteTimeCounter > 0f || jumpBufferCounter > 0f && coyoteTimeCounter > 0f)
{
createDust();
rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
jumpBufferCounter = 0f;
}
if (Input.GetKeyUp(KeyCode.W) && rb.velocity.y > 0f || (Input.GetKeyUp(KeyCode.Space) && rb.velocity.y > 0f))
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
coyoteTimeCounter = 0f;
}
if (Input.GetKeyDown(KeyCode.LeftShift) && canDash)
{
StartCoroutine(Dash());
}
Flip();
if (Input.GetKeyDown(KeyCode.Space) & wallSliding == true || Input.GetKeyDown(KeyCode.W) & wallSliding == true)
{
createDust();
wallJumping = true;
Invoke("SetWallJumpingToFalse", wallJumpTime);
}
}
void SetWallJumpingToFalse()
{
wallJumping = false;
}
private void FixedUpdate()
{
if (isDashing)
{
return;
}
rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
isTouchingFront = Physics2D.OverlapCircle(frontCheck.position, 0.5f, groundLayer);
if (isTouchingFront == true && isGrounded() == false && horizontal != 0)
{
wallSliding = true;
}
else
{
wallSliding = false;
}
if (wallSliding == true)
{
rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue));
}
if (wallJumping == true)
{
rb.velocity = new Vector2(xWallForce * -horizontal, yWallForce);
}
if (rb.velocity.y < 0)
{
rb.gravityScale = gravityScale * fallGravityMultiplier;
}
else
{
rb.gravityScale = gravityScale;
}
if (isGrounded() == true)
{
airborn = false;
}
else if (isGrounded() == false)
{
airborn = true;
}
}
public bool isGrounded()
{
return Physics2D.OverlapCircle(groundCheck.position, 0.5f, groundLayer);
}
private void Flip()
{
if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
{
if (!isGrounded())
{
dust.Stop();
}
else if (isGrounded())
{
createDust();
}
Vector3 localScale = transform.localScale;
isFacingRight = !isFacingRight;
localScale.x *= -1f;
transform.localScale = localScale;
}
}
private IEnumerator Dash()
{
canDash = false;
isDashing = true;
float originalGravity = rb.gravityScale;
rb.gravityScale = 0f;
rb.velocity = new Vector2(transform.localScale.x * dashingPower, 0f);
tr.emitting = true;
createDashExplo();
yield return new WaitForSeconds(dashingTime);
tr.emitting = false;
rb.gravityScale = originalGravity;
isDashing = false;
yield return new WaitForSeconds(dashingCoooldown);
canDash = true;
}
void createDust()
{
dust.Play();
}
void createDashExplo()
{
dashExplo.Play();
}
}
Thanks a lot!