when walljumping I want the player jump to the other direction (away from the wall). Currently my wall check works fine.
I use
bool isCloseToWall = Physics2D.OverlapCircle(transform.position, wallCheckRadius, collisionMask);
for the check.
So in my Update
method I handle the inputs
private void SetMovement() // Input handler in Update
{
float horizontalInput = Input.GetAxisRaw(StringCollection.INPUT_HORIZONTAL);
horizontalInput *= Input.GetKey(KeyCode.LeftShift) ? MOVEMENT_SPEED_RUN : MOVEMENT_SPEED_WALK;
if (Input.GetKeyDown(KeyCode.Space))
{
if (groundCheck.IsGrounded) // Default jump from the ground
{
groundJumpPressed = true;
}
else // **not** grounded
{
if (wallCheck.IsCloseToWall) // jump from the wall
{
wallJumpPressed = true;
}
}
}
movement = new Vector2(horizontalInput, rigid.velocity.y); // set this for the FixedUpdate movement
}
and within my FixedUpdate
I use the movement
Vector and go for this
private void Move()
{
if (groundCheck.IsGrounded)
{
if (groundJumpPressed) // Default jump from the ground
{
movement.y = JUMP_POWER;
groundJumpPressed = false;
}
}
else
{
if (wallCheck.IsCloseToWall)
{
if (wallJumpPressed) // Walljump
{
// jump away from wall
wallJumpPressed = false;
}
}
}
if (movement != Vector2.zero)
{
rigid.velocity = movement; // move the player
}
}
As you can see I have a missing part here when it comes to the wallJumpPressed
part. I want the player jump away from the wall to the other direction.
What do I have to pass in there? I tried
movement.x *= -1; // change the direction
and
rigid.AddForce(new Vector2(-movement.x * jumpPower, jumpPower));
but this did not work.
答案 0 :(得分:0)
这会影响您在空中的移动,但这是我更新它的方式:
private void SetMovement() // Input handler in Update
{
// keep our y velocity on the rigidbody....
movement.y = rigidbody.velocity.y;
float horizontalInput = Input.GetAxisRaw(StringCollection.INPUT_HORIZONTAL);
horizontalInput *= Input.GetKey(KeyCode.LeftShift) ? MOVEMENT_SPEED_RUN : MOVEMENT_SPEED_WALK;
if (Input.GetKeyDown(KeyCode.Space))
{
if (groundCheck.IsGrounded) // Default jump from the ground
{
groundJumpPressed = true;
}
else // **not** grounded
{
if (wallCheck.IsCloseToWall) // jump from the wall
{
wallJumpPressed = true;
}
}
}
if (groundCheck.IsGrounded) // Default jump from the ground
{
// only instantly override x when we are on the ground.
movement.x = horizontalInput;
}
else // **not** grounded
{
// since we are in the air, slowly lower or increase our
// change in direction using time.deltatime
movement.x += horizontalInput * time.deltaTime;
// clamp our values between our max and minimum speed.
movement.x = Mathf.Clamp(movement.x, -MOVEMENT_SPEED_RUN, MOVEMENT_SPEED_RUN);
}
}
private void Move()
{
if (groundCheck.IsGrounded)
{
if (groundJumpPressed) // Default jump from the ground
{
movement.y = JUMP_POWER;
groundJumpPressed = false;
}
}
else
{
if (wallCheck.IsCloseToWall)
{
if (wallJumpPressed) // Walljump
{
// jump away from wall
wallJumpPressed = false;
// negate movement when we jump to the opposite direction
// With changes to SetMovement, we can't just 0 out the
// movement.x since we are in the air.
movement.x *= -1;
}
}
}
if (movement != Vector2.zero)
{
rigid.velocity = movement; // move the player
}
}