Download

Changing character velocity in midair

I’ve spent the last day working on getting a double jump to work and replicate across the network and it’s working pretty well so far. The only problem I’m having now is that I can’t make the double jump change the character’s horizontal velocity. I’d like the character to move in the direction that the controls are held when the second jump is triggered. The problem right now seems to be that the air control value is stopping me from changing Velocity. This is my code so far:


bool UShooterCharacterMovement::DoJump(bool bReplayingMoves)
{
	if (Cast<AShooterCharacter>(GetCharacterOwner())->GetJumpCount() > 1)
	{
		if (!bConstrainToPlane || FMath::Abs(PlaneConstraintNormal.Z) != 1.f)
		{
			Velocity.Z = JumpZVelocity*DoubleJumpHeightMultiplier;

			//Calculate current speed
			FVector horizontalMovement = Velocity;
			horizontalMovement.Z = 0.0f;
			float speed = horizontalMovement.Size();

			//Change direction of velocity in mid-jump
			horizontalMovement = GetPendingInputVector();
			horizontalMovement *= speed;

			Velocity.X = horizontalMovement.X;
			Velocity.Y = horizontalMovement.Y;

			SetMovementMode(MOVE_Falling);
			return true;
		}
	}
	
	return Super::DoJump(bReplayingMoves);
}

Does this look like the AirControl value is stopping me from moving? Or is there another physics value I’d need to change in order to move in the direction I want?

Not sure if you managed to fix this yet, but I have a solution for you and anybody else reading. Instead of getting the PendingInputVector, you could instead get the rotation vector of the direction the player is facing and apply the current velocity to that. E.g.



bool UShooterCharacterMovement::DoJump(bool bReplayingMoves)
{
	if (Cast<AShooterCharacter>(GetCharacterOwner())->GetJumpCount() > 1)
	{
		if (!bConstrainToPlane || FMath::Abs(PlaneConstraintNormal.Z) != 1.f)
		{
			Velocity.Z = JumpZVelocity*DoubleJumpHeightMultiplier;

			//Calculate current speed
			FVector horizontalMovement = Velocity;
			horizontalMovement.Z = 0.0f;
			float speed = horizontalMovement.Size();

			//Get the rotation of pawn
			FRotator rotation = Controller->GetControlRotation();
			FVector rotationVec = rotation.Vector();

			//Apply speed to rotation vector
			rotationVec.X *= speed;
			rotationVec.Y *= speed;

			//Set new movement velocity
			Velocity.X = rotationVec.X;
			Velocity.Y = rotationVec.Y;

			SetMovementMode(MOVE_Falling);
			return true;
		}
	}
	
	return Super::DoJump(bReplayingMoves);
}


Hopefully this should work for you.

Cheers!