Car Rotation while moving backwards

I’m following along with the " Unreal Engine 2D: Make Your Own Epic 2D Games Using C++" course and I can’t ask a question there as I get a message that says " TinyMCE is in read-only mode because you have no more editor loads available this month. Please request that the admin upgrade your plan"

Anyway, I’m at the part where we implement the car movement but when I followed the video the car rotates in the correct direction according to the key pressed but only while moving forward, but if I move backwards the rotation is broken.

Hi and welcome to the community,
I checked and TinyMCE is working again. it happened before.

As for your question, I would recommend first checking your code against the gitlab link. I think what you’re seeing is correct behaviour. Don’t expect it to behave exactly like real cars do (the mechanics for that are really complex) so it may be correct as far as the course is concerned.

Also, share the move function here (don’t screenshot, use the < / > button to format) and I’ll take a look

I followed the code from the video. I am using UE 5.3
here is the whole move function

void APlayerPawn::Move(const FInputActionValue& Value)
{
	if (bCanMove)
	{
		FVector2D MoveActionVal = Value.Get<FVector2D>();
		bool bWantsToMove = abs(MoveActionVal.Y) > 0.0f; // W or S is pressed
		if (bWantsToMove)
		{
			float DeltaTime = GetWorld()->DeltaTimeSeconds;
			bool bWantsToRotate = abs(MoveActionVal.X) > 0.0f; // A or D is pressed
			if (bWantsToRotate)
			{
				float PitchRot = -RotationSpeed * MoveActionVal.X * DeltaTime;
				AddActorWorldRotation(FRotator(PitchRot, 0.0f, 0.0f));
			}
			float FinalMovementSpeed = MovementSpeed;
			const bool bMovingBackwards = MoveActionVal.Y < 0.0f;
			if (bMovingBackwards)
				FinalMovementSpeed *= 0.5f;
			FVector DistanceToMove = GetActorUpVector() * FinalMovementSpeed * MoveActionVal.Y * DeltaTime;
			SetActorLocation(GetActorLocation() + DistanceToMove);
		}
	}
}

Your code is correct. I tested it and it does exactly as mine and also The instructors code. I know what you’re probably thinking, when I go backwards, cars don’t move like that and you’re right but it isn’t meant to be a realistic simulation.

If you are unhappy about this, replace

float PitchRot = -RotationSpeed * MoveActionVal.X * DeltaTime;
AddActorWorldRotation(FRotator(PitchRot, 0.0f, 0.0f));

With

auto RotateDirection = MoveActionVal.X;
if (MoveActionVal.Y < 0 )
{
  RotateDirection *= -1.0f;
}
const auto PitchRot = -RotationSpeed * RotateDirection * DeltaTime;
AddActorWorldRotation(FRotator(PitchRot, 0.0f, 0.0f));

This changes the way rotate works when going backwards which is arguable more like car movement.

yeah of course it’s not a realistic simulation but for a player input experience it’s counter intuitive. But yeah thanks for the heads up.

If you hadn’t pointed it out, I’d have never noticed tbh.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms