How to switch player characters

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?

Thanks in advance

Hey @Retroguy82 how are you?

What you want to do is to “posses” different characters, switchng between them to control the separately, right?

In that case the best you can do is to follow this guide:

This is a really usefull guid and will prevent you from making many mistakes that are normal when trying to do this.

hope this helps you!

1 Like

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?

Hello again @Retroguy82

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.

Let me know if you need more help on this!

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?

Hello again @Retroguy82

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.

With that, your characters should apply physics even if you unposses them!

Let me know if it worked!

1 Like

It worked, great stuff!

There’s lots more stuff I can get around to adding now, but if I run into more issues I hope you can give me a bit more help?

Thanks a lot Juan, it’s much appreciated :stuck_out_tongue:

1 Like

It’s my pleasure!

If you need help with anything else don’t hesitate to create a new post!

Awesome, thanks for your help so far!

1 Like