# How to get rid of desync from crouching (sync timeline in C++)?

**URL:** <https://forums.unrealengine.com/t/how-to-get-rid-of-desync-from-crouching-sync-timeline-in-c/503335>\
**Category:** Multiplayer & Networking\
**Tags:** unreal-engine, UE4-26, question\
**Created:** [March 2, 2022, 8:17am UTC](https://forums.unrealengine.com/t/how-to-get-rid-of-desync-from-crouching-sync-timeline-in-c/503335 "2022-03-02T08:17:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JonahPlusPlus](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JonahPlusPlus](https://forums.unrealengine.com/u/JonahPlusPlus)\
**Post date:** [March 2, 2022, 8:17am UTC](https://forums.unrealengine.com/t/how-to-get-rid-of-desync-from-crouching-sync-timeline-in-c/503335/1 "2022-03-02T08:17:50Z")

</div>

Posting here as well as on the Unreal Engine subreddit (not sure which one is more popular)? So I am creating my own Character and CharacterMovementComponent Classes based off [this wonderful video](https://www.youtube.com/watch?v=RtQRMcupJs0), and wanted to add in smooth crouching, so I override Crouch and UnCrouch with my own implementation that calls Play and Reverse on a Timeline (in C++) that then calls a custom CrouchUpdate event (in BP) which handles lerping the half height and z offset.

The issue I’m running into is that while it looks great, it isn’t synced, even with no latency or packet loss. And then when I turn on 1000 ping and 15% packet loss, movement is slightly off, but when I crouch it goes wild, flip flopping between crouched and uncrouched and getting stuck in the wrong mode.

How do I sync the timeline with no latency or packet loss? And how do I protect against packet loss?

Is it possible that I need include some client prediction code?

Relevant Code (please ask for more if necessary):

```auto
void UFPSCharacterMovementComponent::Crouch(bool bClientSimulation)
{
	if (!HasValidData())
	{
		return;
	}

	if (!bClientSimulation && !CanCrouchInCurrentState())
	{
		return;
	}

	if (CharacterOwner->GetCapsuleComponent()->GetUnscaledCapsuleHalfHeight() == CrouchedHalfHeight)
	{
		if (!bClientSimulation)
		{
			CharacterOwner->bIsCrouched = true;
		}
		return;
	}
        
        // Not sure if necessary
	//if (bClientSimulation && CharacterOwner->GetLocalRole() == ROLE_SimulatedProxy)
	//{
	//	// restore collision size before crouching
	//	ACharacter* DefaultCharacter = CharacterOwner->GetClass()->GetDefaultObject<ACharacter>();
	//	CharacterOwner->GetCapsuleComponent()->SetCapsuleSize(DefaultCharacter->GetCapsuleComponent()->GetUnscaledCapsuleRadius(), DefaultCharacter->GetCapsuleComponent()->GetUnscaledCapsuleHalfHeight());
	//}

	if (!bClientSimulation)
	{
		CharacterOwner->bIsCrouched = true;
	}

	bForceNextFloorCheck = true;

	CrouchTimelineComp->Play();

}

void UFPSCharacterMovementComponent::UnCrouch(bool bClientSimulation)
{
	if (!HasValidData())
	{
		return;
	}

	ACharacter* DefaultCharacter = CharacterOwner->GetClass()->GetDefaultObject<ACharacter>();

	if (CharacterOwner->GetCapsuleComponent()->GetUnscaledCapsuleHalfHeight() == DefaultCharacter->GetCapsuleComponent()->GetUnscaledCapsuleHalfHeight())
	{
		if (!bClientSimulation)
		{
			CharacterOwner->bIsCrouched = false;
		}
		return;
	}

	if (!bClientSimulation)
	{
		// Test for uncrouch

		CharacterOwner->bIsCrouched = false;
	}

	CrouchTimelineComp->Reverse();

}

```

---

<div class="post-metadata">

**Author:** ![JonahPlusPlus](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JonahPlusPlus](https://forums.unrealengine.com/u/JonahPlusPlus)\
**Post date:** [March 10, 2022, 4:19am UTC](https://forums.unrealengine.com/t/how-to-get-rid-of-desync-from-crouching-sync-timeline-in-c/503335/2 "2022-03-10T04:19:21Z")

</div>

Changed it so the capsule is not smoothed, but the camera is smoothed. Essentially, just called the Super implementation and added a call to BlueprintImplementable event that smoothed the camera position. Probably will move that node to be C++ as I reimplement everything with GAS.
