I've got a very annoying problem that I have been stuck on for a few days and I can't figure it out.
It is for a wall jump.
I basically want my wall jump to have a bit of force away from to wall to make it look and feel better than just jumping up straight.
The problem is, when i add a force on the x axis, it does not register when i am also holding the arrow towards the wall. If I am not holding down the walking button, then press jump...then the x force works.
I have a feeling that the horizontal axis force is competing with the opposite force that i want to add to the player when he hits the jump button...
Walking code:
private void ApplyMovement()
{
if (isGrounded)
{
myRigidbody.velocity = new Vector2(movementSpeed * movementInputDirection, myRigidbody.velocity.y);
}
else if (!isGrounded && !isWallSliding && movementInputDirection != 0)
{
Vector2 forceToAdd = new Vector2(movementForceInAir * movementInputDirection, 0);
myRigidbody.AddForce(forceToAdd);
if (Mathf.Abs(myRigidbody.velocity.x) > movementSpeed)
{
myRigidbody.velocity = new Vector2(movementSpeed * movementInputDirection, myRigidbody.velocity.y);
}
}
else if (!isGrounded && !isWallSliding && movementInputDirection == 0)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x * airDragMultiplier, myRigidbody.velocity.y);
}
if (isWallSliding)
{
if (myRigidbody.velocity.y < -wallSlideSpeed)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, -wallSlideSpeed);
}
}
}
Jump code:
private void Jump()
{
if (canJump && !isWallSliding)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
numberOfJumpsLeft--;
}
else if ((isWallSliding || isTouchingWall) && !isGrounded && canJump)
{
isWallSliding = false;
numberOfJumpsLeft--;
Vector2 forceToAdd = new Vector2(wallPushForce * -facingDirection, wallJumpForce *
wallJumpDirection.y);
myRigidbody.AddForce(forceToAdd, ForceMode2D.Impulse);
}
}
Where I call The code:
void Update()
{
CheckInput();
}
private void FixedUpdate()
{
ApplyMovement();
}
private void CheckInput()
{
movementInputDirection = CrossPlatformInputManager.GetAxisRaw("Horizontal");
if (Input.GetButtonDown("Jump"))
{
Jump();
}
if (Input.GetButtonUp("Jump"))
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, myRigidbody.velocity.y * variableJumpHeightMultiplier);
}
}
Any help would be much appreciated.
Thanks in advance