ACharacter GetVelocity returns value in millions? not accurate. How to do it correctly?

if(GetVelocity().SizeSquared()!=0)
		{
			UE_LOG(LogTemp, Warning, TEXT("velocity= %d , %d , %d"), GetVelocity().X, GetVelocity().Y, GetVelocity().Z);
		}

I have this code in my ACharacter extended class Tick function. I have tried several variations such as GetOwner()->GetVelocity.

But the value is wrong. It was supposed to be measured in cm/s but like I say in title it gives values in millions also the values change eratically, although it does appear they stay at zero when they should and start increasing when i move.

I also tried GetCharacterMovement()->GetVelocity aswell as GetMovementComponent()->GetVelocity.

Nothing I tried has worked.

I am guessing it has something to do with how I move. But AFAIK I do it the recommended way (ie using the InputComponent property that is already part of ACharacter):

void ACopCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	PlayerInputComponent->BindAxis("MoveForward", this, &ACopCharacter::MoveForward);
	PlayerInputComponent->BindAxis("MoveRight", this, &ACopCharacter::MoveRight);
	PlayerInputComponent->BindAxis("LookUp", this, &ACopCharacter::LookUp);
	PlayerInputComponent->BindAxis("LookRight", this, &ACopCharacter::LookRight);

	PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
	PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
	PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &ACopCharacter::ActionCrouch);
	PlayerInputComponent->BindAction("Crouch", IE_Released, this, &ACopCharacter::ActionUnCrouch);
	PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ACopCharacter::ActionFireGun);
}
void ACopCharacter::MoveForward(float val)
{
	if((Controller!=nullptr)&&(val!=0.0f))
	{
		AddMovementInput(FRotationMatrix(FRotator(0, Controller->GetControlRotation().Yaw, 0)).GetUnitAxis(EAxis::X), val);
	}
}

void ACopCharacter::MoveRight(float val)
{
	if((Controller!=nullptr)&&(val!=0.0f))
	{
		AddMovementInput(FRotationMatrix(FRotator(0, Controller->GetControlRotation().Yaw, 0)).GetUnitAxis(EAxis::Y), val);
	}

Any help is much appreciated. Thank u

You are printing the values as “integer” using %d, but you are not casting the values you pass to UE_LOG() to “int.”
A floating point value in memory, interepreted as bytes of an integer, will look like a very large integer value.

Thank you very much for spotting this mistake. I will try to change it to float now

…Edit: much better results now lol thanks for the fast help!

if(GetVelocity().SizeSquared()!=0)
		{
			UE_LOG(LogTemp, Warning, TEXT("velocity= %f , %f , %f"), GetVelocity().X, GetVelocity().Y, GetVelocity().Z);
		}

No problem!

If it worked for you, feel free to mark it as “solution” and/or “helpful” :smiley:

1 Like