I have two characters, one is flying in the air, the other is walking on the ground. I want to switch between these two characters in the game. What should I do.
I tried GetController () ->Postess (Character), but my flying character can’t fly.
The flying character needs to have it’s motion programmed inside of it.
You need to add in a movement component (probably Floating Pawn Movement Component) to get it to move.
Or you can program the inputs directly if you just want to use vector math to move it around.
Then once possessed it should start reacting to the movements.
The flying character can run independently. I have coded the flying mode. But if I call GetController () ->Postes (Character) to switch the character I am controlling, the flying character will have a bug. I don’t know how to switch characters correctly
It’s usually enough to un-possess the current character in the controller and possess the new one.
Do this from within the player controller, not the characters.
I found that when getting the player controller, I need to pass in (EAutoReceiveInput:: Type:: Player0 or EAutoReceiveInput:: Type:: Player1). I am using the player controller of player0 to control player1. I wonder if there is something wrong here. However, I didn’t create a player controller. I just created two cpp class, one is a flying character, and the other is an ordinary character.
Is there any way to switch the characters in use without using GetController () ->Postes (Character)
If you want to use c++ then you need to inherit from APlayerController and put your character possession / un-possession code there.
You also have to set this new player controller class in your custom game mode.
Do not use AutoReceiveInput for characters. They should be receiving the information from the player controller.
if you want to do this outside the player controller then you need to use
APlayerController * pc = UGameplayStatics::GetPlayerController(GetWorld(), 0);
and then do the possession / unpossesion with the AplayerController instance.
(personally I would just put this code in the player controller to keep it clean)