Change pawn movement speed

I’m newbie in Unreal development and I don’t know how to move my pawn faster.

I’m developing pong and I have this method to move the pawn:

void AAIPaddle::MovePaddle(float Scale)
{
	// Add a movement input, hardcoding Z speed to 100.0f
	FVector direction = FVector(0.0f, 0.0f, 100.0f);

	// Scale could be -1.0f or 1.0f or 0.0f.
	AddMovementInput(direction, Scale);
	if (OurMovementComponent &&
		(OurMovementComponent->UpdatedComponent == RootComponent))
	{
		OurMovementComponent->AddInputVector(direction * Scale);
		ZVelocity = Scale;
	}
}

You can find the class AAIPaddle here.

I use this FVector direction = FVector(0.0f, 0.0f, 100.0f); to set its speed, but I change the value 100.0f with 400.0f or another greater values and it moves with the same speed.

Maybe I haven’t understood anything. How can I change its movement speed?

I’m using the following class that inherits from UPawnMovementComponent.

void UPaddlePawnMovementComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	// Make sure that everything is still valid, and that we are allowed to move.
	//		PawnOwner: which pawn owns this MovementComponent.
	//		UpdatedComponent: the component we move and update.
	//		ShouldSkipUpdate: Possibly skip update if moved component is not rendered or can't move.
	// If none own this component or there isn't anything to move or we have to skip this update, then return.
	if (!PawnOwner || !UpdatedComponent || ShouldSkipUpdate(DeltaTime))
	{
		return;
	}

	// Get (and then clear) the movement vector that we set in ACollidingPawn::Tick
	FVector DesiredMovementThisFrame = ConsumeInputVector().GetClampedToMaxSize(1.0f) * DeltaTime * 150.0f;
	// If movement isn't nearly zero
	if (!DesiredMovementThisFrame.IsNearlyZero())
	{
		FHitResult Hit;
		// Move our UpdatedComponent by the given DesiredMovementThisFrame, and sets rotation to
		// UpdatedComponent->GetComponentRotation(), do a bSweep and store any hit on Hit.
		SafeMoveUpdatedComponent(DesiredMovementThisFrame, UpdatedComponent->GetComponentRotation(), true, Hit);

		// If we bumped into something, try to slide along it
		if (Hit.IsValidBlockingHit())
		{
			StopMovementImmediately();
		}
	}
};

The project is public in Github.

Thanks. I chosen UPawnMovementComponent because I’m new in Unreal development and it was the first one I found.

Do I have to use UFloatingPawnMovement or continue using UPawnMovement? Sorry, but I don’t understand your answer.

IIRC The input vectors are normally used only to determine direction. They are normalized and all movement is then applied in the resulting direction.

Is there any specific reason you are inheriting from UPawnMovementComponent? Without knowing much about your project I would say you should instead inherit from UFloatingPawnMovement.

Using UFloatingPawnMovement exposes variables like Acceleration and Max Speed.

Looking a little more at your code when you consume the input vector you use:

ConsumeInputVector().GetClampedToMaxSize(1.0f) * DeltaTime * 150.0f;

If it wasn’t normalizing the vector before it is definitely normalized after GetClampedToMaxSize(1.0f).

I haven’t read the code for UPawnMovementComponent, so I am not certain if it does the normalization itself. Either way, for the sake of code organization I believe it would be better not to try to tell the movement component how fast to go through the input vectors. This could give rise to strange problems like if you set up two methods of control WASD and arrow keys a player could move twice as fast by pressing W and the up arrow.

You could expose your own variable on your UPaddlePawnMovementComponent that represents speed and then multiply the vector by that instead. Then you could do something like:

ConsumeInputVector().GetClampedToMaxSize(1.0f) * DeltaTime * Speed;

1 Like

Sorry, I’ve gone back and edited my answer like 5 times now. You may want to look at it again. >.<

1 Like

Ok. I have understood everything. Thanks for your answer.