Attach Player to Physics Object In Multiplayer

Hi everyone,

For my game I wanted to implement a little Easter Egg where if the player uses a toboggan (a physics object) they will get on it and ride. It’s a very simple premise: the toboggan is simulating physics with a physical material that has very low friction (0.1 I think).The toboggan is set to always replicate and replicates movement. The player can run into it to push it and then press E to jump on and ride it down the slope.

The system works really well for the server and the owning client, but observing clients see this:

https://i.gyazo.com/77bedf0a63db9f7e5ca3c0de633d5b84.gif

It looks like on the client side they are still trying to simulate gravity and the guy is constantly falling through the floor and being corrected by the server or something.

Here is my code for the RPC that the owning client issues to start riding the toboggan (made it reliable):



void ABaseCharacter::Server_RideToboggan_Implementation(AToboggan* toboggan) {
    if (HasAuthority()) {
        if (bIsTobogganing || !toboggan) {

            this->GetCapsuleComponent()->DetachFromComponent(FDetachmentTransformRules::KeepWorldTransform);
            this->SetActorEnableCollision(true);
            bIsTobogganing = false;
            riddenToboggan = nullptr;

        }
        else {
            this->SetActorEnableCollision(false);
            this->GetCapsuleComponent()->AttachToComponent(toboggan->GetRootComponent(), FAttachmentTransformRules::SnapToTargetNotIncludingScale);
            bIsTobogganing = true;
            riddenToboggan = toboggan;
        }
    }
}


riddenToboggan is a replicated pointer to a Toboggan object, and has the following OnRep function defined:



void ABaseCharacter::OnRep_IsTobogganing() {
    if (riddenToboggan) {
        this->SetActorEnableCollision(false);
        this->GetCapsuleComponent()->AttachToComponent(riddenToboggan->GetRootComponent(), FAttachmentTransformRules::SnapToTargetNotIncludingScale);
    }
    else {
        this->GetCapsuleComponent()->DetachFromComponent(FDetachmentTransformRules::KeepWorldTransform);
        this->SetActorEnableCollision(true);

    }
}


The system produces the same issue if I don’t try attaching in the OnRep function, and only set the collision (I have to set collision client-side regardless as it doesn’t seem to be replicated by default, leading to interesting craziness on the client side!).

Can anyone explain to me what I’m doing wrong here? I don’t understand why the owning client works perfectly and not for other clients since they all run the same code.

I managed to resolve the issue myself, it turns out I had to set the character movement component’s movement mode to MOVE_None when they were supposed to be attached to the toboggan, and back to MOVE_Walking when they dismount.



void ABaseCharacter::Server_RideToboggan_Implementation(AToboggan* toboggan) {
    if (HasAuthority()) {
        if (bIsTobogganing || !toboggan) {

            this->GetCapsuleComponent()->DetachFromComponent(FDetachmentTransformRules::KeepWorldTransform);
            this->SetActorEnableCollision(true);
            this->GetCharacterMovement()->MovementMode = EMovementMode::MOVE_Walking;
            bIsTobogganing = false;
            riddenToboggan = nullptr;

        }
        else {
            this->SetActorEnableCollision(false);
            this->GetCapsuleComponent()->AttachToComponent(toboggan->GetRootComponent(), FAttachmentTransformRules::SnapToTargetNotIncludingScale);
            this->GetCharacterMovement()->MovementMode = EMovementMode::MOVE_None;
            bIsTobogganing = true;
            riddenToboggan = toboggan;
        }
    }
}


My guess is that the observing client was still simulating movement via the character movement component, which caused them to continuously mess with the character’s position on the toboggan. By disabling it, movement simulation is disabled and the character stays firmly in place.

Thanks for reporting back with the solution, you saved me a lot of frustration. THANKS :slight_smile: