p.UseTargetVelocityOnImpact

p.UseTargetVelocityOnImpact

#Overview

name: p.UseTargetVelocityOnImpact

This variable is created as a Console Variable (cvar).

It is referenced in 3 C++ source files.

#Summary

#Usage in the C++ source code

The purpose of p.UseTargetVelocityOnImpact is to control how velocity is recalculated after an impact in character movement, particularly when colliding with physics objects.

This setting variable is primarily used in the Character Movement Component of Unreal Engine’s physics system. It’s specifically utilized in the PhysFalling function, which handles the physics of a character falling.

The value of this variable is set using an FAutoConsoleVariableRef, which allows it to be modified at runtime through console commands. By default, it’s set to 1 (enabled).

The associated variable UseTargetVelocityOnImpact directly interacts with p.UseTargetVelocityOnImpact. They share the same value and are used interchangeably in the code.

Developers must be aware that this variable significantly affects how character velocity is calculated after collisions, especially with moving objects. When enabled (set to 1), it improves the accuracy of velocity calculations when colliding with moving physics objects.

Best practices when using this variable include:

  1. Keep it enabled (set to 1) for most scenarios, especially in games with complex physics interactions.
  2. If you’re experiencing unexpected behavior in character movement after collisions, particularly with moving objects, consider checking this variable’s state.
  3. Be cautious when disabling it, as it may lead to incorrect velocity calculations when interacting with moving physics objects.

Regarding the associated variable UseTargetVelocityOnImpact:

#References in C++ code

#Callsites

This variable is referenced in the following C++ source code:

#Loc: <Workspace>/Engine/Source/Runtime/Engine/Private/Components/CharacterMovementComponent.cpp:266

Scope (from outer to inner):

file
namespace    CharacterMovementCVars

Source code excerpt:

	static int32 UseTargetVelocityOnImpact = 1;
	FAutoConsoleVariableRef CVarUseTargetVelocityOnImpact(
		TEXT("p.UseTargetVelocityOnImpact"),
		UseTargetVelocityOnImpact, TEXT("When disabled, we recalculate velocity after impact by comparing our position before we moved to our position after we moved. This doesn't work correctly when colliding with physics objects, so setting this to 1 fixes this one the hit object is moving."));

	static float ClientAuthorityThresholdOnBaseChange = 0.f;
	FAutoConsoleVariableRef CVarClientAuthorityThresholdOnBaseChange(
		TEXT("p.ClientAuthorityThresholdOnBaseChange"),
		ClientAuthorityThresholdOnBaseChange,

#Associated Variable and Callsites

This variable is associated with another variable named UseTargetVelocityOnImpact. They share the same value. See the following C++ source code.

#Loc: <Workspace>/Engine/Source/Runtime/Engine/Private/Components/CharacterMovementComponent.cpp:264

Scope (from outer to inner):

file
namespace    CharacterMovementCVars

Source code excerpt:

		TEXT("If enabled, character velocity corrections will be treated as relative to dynamic movement bases."));

	static int32 UseTargetVelocityOnImpact = 1;
	FAutoConsoleVariableRef CVarUseTargetVelocityOnImpact(
		TEXT("p.UseTargetVelocityOnImpact"),
		UseTargetVelocityOnImpact, TEXT("When disabled, we recalculate velocity after impact by comparing our position before we moved to our position after we moved. This doesn't work correctly when colliding with physics objects, so setting this to 1 fixes this one the hit object is moving."));

	static float ClientAuthorityThresholdOnBaseChange = 0.f;
	FAutoConsoleVariableRef CVarClientAuthorityThresholdOnBaseChange(
		TEXT("p.ClientAuthorityThresholdOnBaseChange"),
		ClientAuthorityThresholdOnBaseChange,
		TEXT("When a pawn moves onto or off of a moving base, this can cause an abrupt correction. In these cases, trust the client up to this distance away from the server component location."),

#Loc: <Workspace>/Engine/Source/Runtime/Engine/Private/Components/CharacterMovementComponent.cpp:4888

Scope (from outer to inner):

file
function     void UCharacterMovementComponent::PhysFalling

Source code excerpt:

				// Compute velocity after deflection (only gravity component for RootMotion)
				const UPrimitiveComponent* HitComponent = Hit.GetComponent();
				if (CharacterMovementCVars::UseTargetVelocityOnImpact && !Velocity.IsNearlyZero() && MovementBaseUtility::IsSimulatedBase(HitComponent))
				{
					const FVector ContactVelocity = MovementBaseUtility::GetMovementBaseVelocity(HitComponent, NAME_None) + MovementBaseUtility::GetMovementBaseTangentialVelocity(HitComponent, NAME_None, Hit.ImpactPoint);
					const FVector NewVelocity = Velocity - Hit.ImpactNormal * FVector::DotProduct(Velocity - ContactVelocity, Hit.ImpactNormal);
					Velocity = HasAnimRootMotion() || CurrentRootMotion.HasOverrideVelocityWithIgnoreZAccumulate() ? FVector(Velocity.X, Velocity.Y, NewVelocity.Z) : NewVelocity;
				}
				else if (subTimeTickRemaining > UE_KINDA_SMALL_NUMBER && !bJustTeleported)