Two "characters" using one manager and/or state machine?

So I have two “characters” being controlled by the player using a single gamepad. Each character can move around using the respective thumbstick and can either pick up and item or drop an item by holding or releasing it’s respective shoulder button. So far I have used a single “Character.cs” file with a enum to track which character it is and then using that to determine input. So one character has a Side.LEFT and the other a Side.RIGHT and using that for input. And both characters have a state machine to switch between idle, pickup, grabbing, drop, etc. Both “agents” manage themselves and this works well.

The problem comes when needing these two characters to interact with each other. For example swapping items between characters. So if the Left character is holding an item and wants to give it to the Right character. First the Right character would have to move over near the Left character and hold it’s grab button and the Left character would then need to release its grab button to complete the swap. Implementing this I’ve come across multiple issues and a lot of spaghetti code. Mainly having to reference the other character from one characters class. I have a GetOtherHand() method the returns the “other” hand… lol

But it doesn’t solve my main problem which is I think that both characters should be controlled by one object? Otherwise swapping objects between characters and basic interaction with them is nearly impossible.

So I’m wondering if anyone has any insight on how to implement two characters with interactivity between them? Should I continue down this self-managed character or implement a CharacterController which would handle the collisions and swapping?

I could add code here but I don’t think any code would help explain the issue.

TLDR: How should two player controlled characters be implemented? Each character being a self-managed state machine or having a single character controller to manage the states of the characters?

Thanks!

Well this simple question really depends on the details of your problem. So really, tldr isn’t enough here… we need the previous. Because the design depends on your needs.

As for your needs.

Well…

I think I might need more info, but working with what I got.

You have 2 characters, both controlled by one player? One controller input?

Can you control both simultaneously? Or does one swap between the players? Also how does one hold the ‘grab’ button for one character, and release it for another? Is there a LEFT GRAB and RIGHT GRAB buttons independent of one another? (like maybe left trigger is left grab, and right trigger is right grab?)

I would definitely have independent state machines for each character’s state. If one can be grabbing something while another is idling… then yeah, you need separate state machines for each (these can be sub-state machines of a single larger one… but still they MUST be independent).

But as for the overall control from the ‘inputs’… I don’t even fully understand your User Interface well enough to say exactly.

I would say you have a single state machine for this where you’re swapping between the states of ever Side.Left or Side.Right. But the fact you describe controlling both states simultaneously confuses me… this doesn’t sound like a state machine at all… since both are doing stuff at the same time.

Thanks for replying. Let me attempt to explain the problem better and more thoroughly.

There are two characters both controlled by one player and both on the same gamepad controller. The “left” character uses the left analog stick to move horizontally on the xz plane. And the “right” character does the same but using the right analog stick. And really these aren’t “characters” but floating hands. I’m trying to simulate swapping items between hands.

The player uses the left and right bumpers on the gamepad to pickup and drop items on a grid by holding the bumper to pickup and hold the item and release the bumper to drop the item. The input isn’t really much of an issue.

All of this is well and I have it working, though it’s sloppy. The problem mainly shows it self with the state machine on each character/hand with states like empty, pickup, grab, drop and swap.

Each character needs to be aware of the others state in some of it’s own states. Like for swapping. But is this right? Should one character need to know the state of the other? Or would that be more suited to a manager of some sort?

Basically inside the Grab state I have something like…

“Is the distance between myself and the other hand close enough to change to Swap state AND is the other hand in the Empty state?”

This doesn’t seem to correct. Maybe expanding the number of states I have, or just splitting the Swap state into two separate states like Give and Get?

A few screenshots for an example.

Why doesn’t this sound correct? It sounds fine enough to me.

Expanding the number of states? What, like having a ‘Grab&Near’ state? What is the transition into this state? That sounds cumbersome honestly. I wouldn’t go about it this way at all.

I could definitely see a ‘Give’ and ‘Get’ state. That describes your situation much better. Since one hand gives, and another receives… there’s no give<->give pairing.

I think my issue was entirely with states. After some thought I think I have come up with a better state machine for the interaction between the hands.

Does this look solid? When swapping the Empty hand goes into the Get state when close to the other hand when in the Grab state. This makes the Grabbed hand transition to the Give state. At this point the player can move the hands apart to make the hand in the Get state transition to the Empty state, which the other hand in the Give state notices and then transitions to the Grab state. If that makes any sense whatsoever.