How to make a pawn move properly? (C++)

I’ve been trying to figure out how to make my pawn properly so that it does not do this. I’m mostly following tutorials so I tried using AddMovementInput but the pawn wouldn’t move, same thing as AddInputVector (I assume I might have missed out some things?). I did not save the code but there was one where the pawn would just face-plant into ground rather than moving. I don’t know how to post my code so I’ll just attach screenshot here.
Annotation 2022-11-03 130902

I’ve also noticed that changing some stuff in the movementcomponent does not affect the pawn at all, should the pawn move using the movement component?
Anyways, I’d really appreciate it if anyone could help!

I think the reason why AddMovementInput or AddInputVector did not work is because I had simulate physics on. I want to make a physics based movement so idek what to do at this point.

If you want physics movement, add forces based on input.
Don’t use SetActorLocation.
Should be looking like

void AMyPawn::Move_FB(float axis)
{
    if (axis != 0)
    {
        if (auto Comp = Cast<UPrimitiveComponent>(GetRootComponent()))
	{
            Comp->AddForceAtLocationLocal(FVector::XAxisVector * axis * speed, FVector::ZeroVector);
        }
    }
}

You’ll probably want to implement your own friction forces as well so you don’t accelerate infinitely. Generally takes the form of -F x Velocity to -F x Velocity²

void Tick(float dt)
{
    Super::Tick(dt);
    
    auto Comp = Cast<UPrimitiveComponent>(GetRootComponent());
    FVector V = GetVelocity();
    if (Comp && !V.IsNearlyZero())
    {
            Comp->AddForceAtLocation(-Friction * V.Size() * V.GetSafeNormal(), GetActorLocation());
    }
}

What value would i use for the friction? Also the pawn does not move when i tried using the one you commented.

huh, this might be caused by something else, i created a new pawn to do this but it’s not doing anything at all

Sorry if this counts as spam but I got the pawn to work again but the code you suggested did not work :confused:

Make sure the root component is the one that simulates physics.
Otherwise you need to apply forces to the appropriate component.

The mass of the object can play a huge factor, multiply your forces by that amount. A simple 1x1x1 cube defaults to 177kg in mass by the engine. So you need a force of like 10000 if you want to see something move.

Alternatively you can use the “bAccelChange” parameter to ignore mass, and work with more reasonable values, but this parameter is not available in every function.

If you use gravity you’ll have a very strong drag force against you when on ground (imagine pushing a 177kg container on the floor). You’ll probably want to code some mechanism to work around that.

Finally, you don’t really need to code friction because engine provides linear/angular damping properties in the Physics section, which do basically the same.

I can’t seem to get it working. I was assuming that it would be similar to add force from unity but this is quite confusing.

PhysicsPawn.cpp (5.5 KB)
PhysicsPawn.h (1.8 KB)
I’m gonna stop working for today but this is my code so far, I tried a few changes before saving so it might not look like what you expect + i’m a beginnner :sweat_smile:

So your speed force is 1 and your friction force is 10000 that’s a first problem. Remove friction you don’t really need it, you can play around with builtin damping parameters later on.

Second, code in add force is wrong I wonder how it even compiles :

CapsuleComponent->AddForceAtLocationLocal(FVector::XAxisVector, (axis * speed, FVector::ZeroVector));

Change to

CapsuleComponent->AddForceAtLocationLocal(FVector::XAxisVector * axis * speed, FVector::ZeroVector);

With a speed of 100000 and it works… for as long as you are in the air or disable gravity.

Sorry for the late reply, been really sick for the past few days. So it was a syntax error. How can I make it so that it moves normally without having to be mid air? Like make it start moving on directional input.

Turns out that I was not applying enough force. I added * 5 as an example to see if anything would change and it’s moving now even when it’s not mid air.

void APhysicsPawn::Move_FB(float axis)
{
if (axis != 0.0f)
{
if (CapsuleComponent)
{
CapsuleComponent->AddForceAtLocationLocal(FVector::XAxisVector * 5 * axis * speed, FVector::ZeroVector);
}
}
}

It sort of feels off but i’ll keep looking into this.