Download

Replication issues attaching a character to a vehicle

Hi,

I’m trying to attach a character to a vehicle in a multiplayer game. The problem is the character doesn’t always attach right for non-owner clients. Depending on the exact attachment method I use, occasionally the character is offset or totally missing when the vehicle comes into view. I suspect it’s a relevancy issue of some kind but I have no idea what since all pawns are set to always relevant, and neither the vehicle or character meshes have LODs.

I’m using the character “Hero” from the shooter game. Weapons are modified due to refactoring for vehicle weapons, but besides that I haven’t changed much, if anything.

For all clients, even the owner, I use a rep’d var “MyVehicle” for the player’s current vehicle in the PlayerState object. When it replicates I put the player’s character into the vehicle.

Each seat has a socket and I’ve tried attaching the character’s root component, the character actor itself, and just the 3p mesh by itself. All with similar results; it usually works, but occasionally doesn’t.

character vs vehicle collisions are turned off, and SetActorEnableCollision(false) is called on the character before attaching

Once it’s attached I set the character mesh to EAnimationMode::AnimationSingleNode, set the animation and play it. It’s currently using the Hero’s equip or reload anim depending on the seat.

On the server I attempt to stop movement replication with this:

charPawn->GetCharacterMovement()->DisableMovement();
charPawn->GetCharacterMovement()->bEnablePhysicsInteraction = false;

It seems like I’ve tried just about everything… any ideas?

TIA!

-Ken

The characters ‘Attach Parent’ is replicated as part of FRepAttachment.

If you attach the character capsule to the vehicle (server-side only), and the vehicle is a replicated object, then it should just work. If you want to trigger animations, you can override OnRep_AttachmentReplication.

Thanks, but it’s not working. Tried all 3 of these (not at the same time):

charPawn->GetCapsuleComponent()->AttachTo(mainVehicle->RootComponent, seatInfo->SeatCharSocket, EAttachLocation::SnapToTarget, false);
charPawn->GetCapsuleComponent()->AttachTo(mainVehicle->GetMesh(), seatInfo->SeatCharSocket, EAttachLocation::SnapToTarget, false);
charPawn->GetCapsuleComponent()->AttachToComponent(mainVehicle->GetMesh(), FAttachmentTransformRules::SnapToTargetIncludingScale, seatInfo->SeatCharSocket);

I guess I must’ve changed something crucial from the stock code, I’ll have to go over it with a fine toothed comb tonight.

Just so I understand, your code works fine if you run it in a standalone game but the attachment fails on the SimulatedProxys but work on the AutonomousProxys when you run on dedicated server?

I believe they are saying it works on Authority only. Fails on everything else.

Right, on authority only, haven’t tried a dedi server… BUT I just found the problem with the capsule attach replication. I’d overridden OnRep_AttachmentReplication without calling Super on it… I’m going to add the animation trigger to that and I’ll report back…

Ok, so it’s a lot better after I fixed OnRep_AttachmentReplication (added the Super call as it should be) and added the animation trigger there. BUT, now on the owner client (only?), the character disappears when you drive too far from where you entered the vehicle. If you drive back in that general area it reappears. This doesn’t seem to happen on a non-owner client, but I haven’t tested extensively.

Obviously it’s some type of spacial relevancy, so I’m going to look into:

  • maybe the owner’s controller needs to be periodically set to the vehicle’s location?
  • does DisableMovement on the character’s movement has something to do with it?

I just stumbled on this and I’m having the issue described by FrostyElkArne. When a player enters a vehicle, the character snaps to where it’s supposed to be and welds to the socket properly on both the server and the controlling client. On other clients, however, the character snaps to the right place, but then immediately falls through the floor. Here’s the code I’m running when a player tries to enter a vehicle:

        EnterableVehicle->SetDriver(this);
        GetController()->Possess(EnterableVehicle);
        AttachToActor(EnterableVehicle, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), FName("DriverSeat"));
        GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision);

The Character Movement code is still running, it doesn’t care what the character is attached to. You’re disabling collision but gravity is still being applied, so the character will fall through the floor.

In our projects we’ve found that we’ve had to make some customizations to the character to not perform movement when attached to something, and to skip replaying moves when attached.

That is exactly what was happening. Thank you so much! I’ve been looking for an answer for this for quite a few days now. I had posted a question about it on the AnswerHub here but didn’t get any answers. If you want the answer credit for it there you can go ahead and just post that the CharacterMovementComponent is still running and I’ll accept the answer and add the code that I used to fix it. If not I’ll go and add my own answer in a few days to help out future developers. Thanks again so much @TheJamsh!