I have a pawn that’s possessed by Player 1. The pawn is using CharacterMovementComponent to move. If Player 2 possesses the pawn it loses all of its acceleration that it had from Player 1 and starts from 0 again. Is there any easy way to carry over the movement when switching pawn possession between Player Controllers?
I guess you could cache the velocity of the character before player 1 unpossesses, and apply it back right after player 2 possessed it?
I tried this approach a bit with poor results. But I might just need to test it a bit more. Cause there’s no easy way to set acceleration directly to a specific value… Has to be done through AddMovementInput. So makes it a bit more complicated, but in theory it should work, you’re right.
You could use ACharacter::LaunchCharacter() with the XYZ overrides set to true. Also you can dig through the source code of the movement component, I saw a RequestDirectMove() function in there, that could work as well.
/**
* Set a pending launch velocity on the Character. This velocity will be processed on the next CharacterMovementComponent tick,
* and will set it to the "falling" state. Triggers the OnLaunched event.
* @PARAM LaunchVelocity is the velocity to impart to the Character
* @PARAM bXYOverride if true replace the XY part of the Character's velocity instead of adding to it.
* @PARAM bZOverride if true replace the Z component of the Character's velocity instead of adding to it.
*/
UFUNCTION(BlueprintCallable, Category=Character)
virtual void LaunchCharacter(FVector LaunchVelocity, bool bXYOverride, bool bZOverride);
Yess! That actually made it work PERFECTLY. Thank you for the idea.
This was the solution:
FVector LastVelocity = MainCharacter->GetCharacterMovement()->Velocity;
PlayerController->Possess(MainCharacter);
MainCharacter->GetCharacterMovement()->Launch(LastVelocity);