Tank not moving (or moving wrong) when movement called from OnHit

EDIT: Found the problem, and it’s a problem that a lot of people can have. Previously, without the on hit, the player tank moved perfectly, but the AI tanks were always applying force, so they just flew off. With this solution, the AI tanks seemed to work well, but not mine. That seems to be due to the terrain. On flat surfaces the Tank works perfectly, so the bad control problems are caused due to the imperfections of the terrain. Not always the Tracks were touching the ground, that’s why the erratic behaviour.

I’m having problems with the last section of the Tanks, with the suspension, the suspension does not attach properly. Due to other bugs (The tank always lost his components when compile) I’ve remade a new TankBlueprint and I will see if that fixes the suspension problems.

OLD COMMENT:
Once reached this point in the course, if the functions of On Hit (DriveTrack, ApplySidewaysForce, and reset Throotle) are called from there, the tank doesn’t move, but Unreal enter and executes properly all of them, but when they are called from SetThrottle, making the game follow the same logic that was before, it works perfect (except for applying forces when they are not touching the ground, which is the point of the whole video).

When the calls are made from OnHit, if the max throotle force is increased, it gets to move, but the movement is erratic, it moves by itself to the sides, and it only reacts to the movement in one input (You have to let go forward and start moving right, pressing both buttons doesn’t work)

Here is the code:

include "TankTrack.h"

UTankTrack::UTankTrack()
{
    PrimaryComponentTick.bCanEverTick = false;
}

void UTankTrack::BeginPlay()
{
    Super::BeginPlay();
    OnComponentHit.AddDynamic(this, &UTankTrack::OnHit);
}

void UTankTrack::OnHit(UPrimitiveComponent *HitComponent, AActor *OtherActor, UPrimitiveComponent *OtherComponent, FVector NormalImpulse, const FHitResult &Hit)
{
    //UE_LOG(LogTemp, Warning, TEXT("You have reached top speed"));
    DriveTrack();
    CorrectSidewayForce();
    currentThrottle = 0;
}

void UTankTrack::CorrectSidewayForce()
{
    auto slippageSpeed = FVector::DotProduct(GetRightVector(), GetComponentVelocity());

    auto DeltaTime = GetWorld()->GetDeltaSeconds();

    auto correction = slippageSpeed / DeltaTime * -GetRightVector();
    auto tankRoot = Cast<UStaticMeshComponent>(GetOwner()->GetRootComponent());

    tankRoot->AddForce(correction * tankRoot->GetMass() / 2); //There are two tracks, so the force must be divided
}

void UTankTrack::SetThrottle(float throttle)
{
    currentThrottle = FMath::Clamp<float>(currentThrottle + throttle, -1.0f, 1.0f);
    //DriveTrack();
    //CorrectSidewayForce();
    //currentThrottle = 0;
}

void UTankTrack::DriveTrack()
{
    UE_LOG(LogTemp, Warning, TEXT("You have reached top speed"));
    auto forceApplied = GetForwardVector() * currentThrottle * trackMaxDrivingForce;
    auto tankRoot = Cast<UPrimitiveComponent>(GetOwner()->GetRootComponent());

    tankRoot->AddForceAtLocation(forceApplied, GetComponentLocation());
}

UFUNCTION() is set before the OnHit function in the header file.

Anyone knows how this could be solved?

Thank you for your time.

Sounds like it may be a more involved issue to debug. Would you mind sending your project preferably through a git repository?

I’ve uploaded all my code (I’m having issues uploading the whole project due to external assets). If needed I can provide the project in a zip, but it’s size is 5Gb

Well I wanted to debug your project which I can’t do if it’s just the code.

Ok, here it is. The tank tracks are being setup on the tank construction blueprint due to a bug that made them reset every time the code was compiled.

I wanted to thank you in advance, and for the course. As an unity developer is helping me a lot, I never thought that the transition to unreal could be so complicated, as everyone seems to love unreal.

https://mega.nz/#!agMBgCoL!wozrCq2loq69uqCuxNEzsatBytEcyxEUE7CIowq2EHQ

And this is supposed to be in a broken state? Could you show a video of the problem by any chance because I’m not noticing anything wrong with the movement

Edit: When using the code only on the OnHit event, te tank didn’t move at all, but it seems that I solved that part.

I’ve been workin on my side too and it seems that coping your code from the git repository into the RequestDirectMove of the UTankMovementComponent solved the problem (a couple plroblems, the AI tanks where flying of due to previously applying force while being on the air).

auto TankForward = GetOwner()->GetActorForwardVector().GetSafeNormal();
    auto AIForwardIntention = moveVelocity.GetSafeNormal();

    auto ForwardThrow = FVector::DotProduct(TankForward, AIForwardIntention);
    IntendMoveForward(ForwardThrow);

    auto RightThrow = FVector::CrossProduct(TankForward, AIForwardIntention).Z;
    IntendTurnRight(RightThrow);

But now, the Tank barely rotates while going forward or backwards, it only rotates at an appropriate speed while no other velocity is given.

Since it sounds mostly usable now I would suggest you just continue so you don’t waste time debugging this further as the movement is completely redone at the end.

1 Like

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

Privacy & Terms