Trouble manually moving player in C++

I’m trying to take control of the player and manually move them in a straight line to a predetermined point with interpolation.

The way I’m trying to do this is with a CustomMovementComponent attached to the FirstPersonPlayer, which has a custom movement mode. Everything related to the component and the mode seems to be working, at least at a high level.

I added a TickComponent method to my movement component and added new movement code there.

void UGrappleMovementComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	if (MovementMode == MOVE_Custom && CustomMovementMode == MOVE_Grapple)
	{
		ACharacter* character = GetCharacterOwner();
		FVector test_direction = FVector(1, 0, 0);
		character->AddMovementInput(test_direction, 4.0, true);
	}
	else
	{
		Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
	}
}

My expectation is that when the movement mode changes, the user will lose control of the player and the player will move (in this case, on the X axis). So far, the player isn’t going anywhere. Not sure what, if anything, I’m doing wrong here. Is there another, better way to move a character in code and without using the AI navmesh?

Since the previous code wasn’t working, I’ve tried using this:

FVector pos = character->GetActorLocation();
FVector new_pos = FVector(pos.X + 1, pos.Y, pos.Z);
character->SetActorLocation(new_pos);

However, the player now clips through walls, which I was afraid of. Simulating movement through a series of teleports, in my experience, is always buggy. Preferably, I’d like to add movement input or set velocity so we’re not bypassing collisions or physics.

If the player doesn’t have control, I don’t think adding movement input will do anything. SetActorLocation() has an overload option to sweep for collision before attempting to move, with the last bool being an option to teleport with your momentum or not:

FHitResult HitResult;
character->SetActorLocation(new_pos, true, HitResult, true);

You could also try using AddImpulse to the character instead, launching them with a certain velocity.

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