There is a level in my game in which player character has to wear a diving suit, but, with how differently player behaves in a diving suit (physics, movement speed, animation etc., I decided to create a separate player controller for this section. The issue I’m having is that I don’t know how to cast to the new player blueprint.
I use “possess” node to switch to the new pawn, but when it seems that I cannot cast to it, which is a problem when things like control settings, sensitivity and post process values are set to player using casting from options menu.
This is what I have right now: Left one is casting to my original player (this one works) right one is casting to a diving player (which seemingly doesn’t actually cast any values)
EDIT: Someone on Reddit pointed out that the second cast cannot succeed unless the first one fails. Dragging the execution pin from “Cast failed” instead fixed my issue.
You talk about a separate player controller, but then you’re also talking about a separate pawn.
Which one is it?
The code that you’re showing will never work, unless BP_FirstPersonCharacterDIVING is a subclass of BP_FirstPersonCharacter. And, even so, if you create a new player controller, you also need to destroy the pawn actor and spawn a new pawn actor for the class of the player pawn to change.
In general, you typically don’t change the player controller, because the player controller represents the player, and the player doesn’t change – they don’t plug in a new game controller, or swap out their keyboard and mouse. The pawn, however, has different physical simulation and different animation and display, and thus typically can be changed out.
So, keep the same player controller, but de-spawn the current pawn and spawn the new pawn. At that point, casting to the new pawn type will work.
In general, you will use some kind of interface from the player controller to the pawn. The standard bits (“move forward,” “jump,” “crouch” etc) are all already handled by the CharacterMovementComponent, so you can use that by casting the player pawn to Character (assuming both FirstPersonCharacter and FirstPersonCharacterDIVING subclass Character.) For your own commands, you can define your own interface with the various commands you want the player to be able to do, and call those methods on that interface on the current player pawn.