I’m creating a build with two playable characters, each of whom is quite different ie unique attacks, movement speed, mechanics etc. I’d like them to both spawn in the level together, and you can toggle which one you’re playing as by pressing a button, the one you’re no longer playing will simply stand and receive your attacks.
I’ve tried using the player switching mechanic from Gorka Games, but whilst this does let me switch which character the camera is focused on, it doesn’t let me control them. My guess is that this is something to do with the ‘Event Begin Play’, both of my characters have one of these active and hooked up at the start of the level, and I suspect the engine doesn’t know how to differentiate which one should be controlled?
Is there a way I can introduce some sort of variable outside of the characters, something which says ‘If it’s 0 make this character controllable, if it’s 1 switch to the other’, which I can toggle from either character, and that’s what’s used in place of Event Begin Play to activate their controls?
I’m sure I’ve seen this tutorial before, and believe it’s just for switching instances of the same character? As in, they all follow the same control blueprint, and have the same actions, it just allows you to switch between multiple copies?
To do it with multiple characters from different classes, the best way is to create BP_BaseCharacter and a BP_BasePlayerController.
All your characters, no matter which kind of controllers you want to add to them, should be children of BP_BaseCharacter.
In your BP_BasePlayerController you will create the function to switch characters, getting all the BP_BaseCharacters (instead of ThirdPersonCharacter as in the tutorial) and casting to them as well.
And then, all your PlayerControllers should be children of BP_BasePlayerController, so they will inherit the function to switch characters, at the same time as they are completely different from each other player controller.
Alright that sounds worth a shot, so I’d give the base controller actions like the standard jump/move/camera etc, then stuff like ‘action 1’ which could be something like a gunshot when used in a particular character’s code, a sword slash in another’s?
Update:
Okay so I gave it a shot and was pleased to see it works for character switching and also looks to allow unique mechanics, my issue now though is that the physics stop working for the character being switched from, ie if the first character jumps then I switch they’ll be left floating in the air, so what I still need is a method which will still allow them to take hits and respond correctly to being attacked + gravity when they’re not the active player?
Would this be something to do with making all playable characters children of a single template, which would hand things like that regardless of which one is selected?
The reason for that is Characters stop applying physics when they don’t have an assigned controller.
When you possess a different character, the controller is assigned to it and the previous character movement component stops applying physics.
How to solve it? Easy! Go to your BP_BaseCharacter, click on its Movement Component a search for “Physics” in its details. You will find an option called “Run Physics with No Controller” and set it true.