How do I set a Pawns velocity?

I want to make the pawn move in a set direction based on the mouse location. I DO want it to collide with objects, but I DONT want navigation or object avoidance.

My Pawn is set up based on the Top Down 3rd person c++ code example with the following components:

  • CharacterMovement
  • [ROOT] CapsuleComponent
  • ArrowComponent
  • SkeletalMeshComponent

From what I can see SetPhysicsLinearVelocity should do the trick, but I can’t figure out which component to apply it to.

I’ve tried applying it to the root component but it doesn’t exist in that class (via GetRootComponent). I’ve tried applying it to Mesh but it doesn’t move.

If I apply it to the capsule component, it works but the Pawn rotates/tumbles as if it is getting a torque as well. Here’s the code for that last bit:

UCapsuleComponent* move = (UCapsuleComponent*)GetPawn()->GetComponentByClass(UCapsuleComponent::StaticClass());
    		if (move)
    		{
    			move->SetPhysicsLinearVelocity(FVector(100.0f, 0.0f, 0.0f));
    		}

So my question is, how exactly am I supposed to be setting the velocity of the pawn? If applying it to the capsule component is correct, why that component and not the root or mesh component? Why am I getting a rotation, and how can I avoid that?

It seems the velocity needs to be applied to a UPrimitiveComponent. You have one, the Skeletal Mesh.

So maybe try applying to that?

USkeletalMeshComponent* smc = Cast<USkeletaMeshComponent>(RootComponent); 
smc->SetPhysicsLinearVelocity(YourVector);

I ended up keeping the velocity applied to the capsule, and just adding a restoring force that brought the player back to the upright position.