Many problems and bugs with aiming and movement component

Hello,

I am having some problems with my Tank_BP :

  1. Whenever I stop moving the mouse to control camera rotation and a line trace goes out, the barrel and the turret start shaking vigorously. I do not think this is a problem with the collision because I have tried removing collisions from the meshes. The error began when I created a new level and added a new Tank there. Ever since then, I have been having this issue. Also, since I am in a new level, I have forgotten where to setup the tank player controller as this was done in a couple of lectures back.

I do not think we are supposed to set it up in the project settings in the maps and modes tab but that is where I have set it up currently as such :

This shows the tank hud in all the levels and obviously we do not want that. Where do I set up this tank player controller BP? Is it in the level blueprint? and if so, how do I do it?

  1. Another issue I am having is that the AI Tank is not aiming or moving, and I have almost the same code as the instructor. Here is my tank AI Controller class but like I said, I do not think that the problem is there.
#include "TankAimingComponent.h"
#include "TankAIController.h"

void ATankAIController::BeginPlay() 
{
    Super::BeginPlay();
    AimingComponent = GetPawn()->FindComponentByClass<UTankAimingComponent>();
    // To Do : Look for the nearest tank rather than the player tank everytime
}

void ATankAIController::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
    TargetTank = GetWorld()->GetFirstPlayerController()->GetPawn();
    if (!ensure(TargetTank && AimingComponent)) {return;}
    MoveToActor(TargetTank, AcceptanceRadius);
    Aim();
    AimingComponent->Fire(); // To do : Only fire if the AI Tank is aiming at the player tank
}

void ATankAIController::Aim()
{
    // To do : AI Tank looks for nearby tank NOT player tank
    auto PlayerLocation = TargetTank->GetActorLocation();
    auto Distance = FVector::Distance(PlayerLocation, GetPawn()->GetActorLocation());
    if (Distance < AimingComponent->TankRange)
    {
        AimingComponent->Aim(PlayerLocation);
    }
}

I have refactored the aiming and made it into a function. I have also made it so that it takes into the account the reach of the tank. The instructor does do this for the player tank but not the AI Tank. It only took two lines of code so I did that to make it fair. As you can see, I also have a “TO DO” for where I want the AI Tanks to be able to interact with each other as well rather than just targeting the player tank. I think want an AI Tank to aim at whichever tank is nearest. I was thinking that I could get all instances of ATank in the level, find which one has the minimum distance and choose that one to targeted. Does anyone know how to do that?

Another To Do is to let the ai tank to only fire after they have aimed at the target tank or they fire for no reason. Does anyone have tips for that?

Anyway, I cannot seem to figure why the AI Tank is not aiming or not even moving at the least. If you want to see the tank movement component code, you comment below but i do not think that is the problem.

  1. The last problem I am having is that whenever I recompile my code, firing category in the aiming component of the tank BP resets to default. Meaning that all the settings that I change in the blueprint are reset to the code in C++. This is a big problem, because the aiming component needs a reference to a projectile blueprint and with this issue, I have to select the projectile blueprint in the tank blueprint every time after I compile code. I also change the fire speed (launch speed) to a different value.

Here is what I am talking about:

image

Here is my code in the header file of the aiming component :

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "TankAimingComponent.generated.h"

class UTankBarrel; 
class UTankTurret;
class ATankProjectile;

UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BATTLETANK_API UTankAimingComponent : public UActorComponent
{
	GENERATED_BODY()

public:	
	UTankAimingComponent();

protected:
	UFUNCTION(BlueprintCallable, Category = "Setup")
	void Initialize(UTankBarrel* SetBarrel, UTankTurret* SetTurret);

public:
	void Aim(FVector AimLocation);
	void MoveBarrelAndTurret(FVector AimDirection);
	bool GetAimDirection(FVector &OutAimDirection, FVector AimLocation);

	UFUNCTION(BlueprintCallable, Category = "Firing")
	void Fire();

	UPROPERTY(EditAnywhere, Category = "Firing")
	float TankRange = 1000000;

private:
	UTankBarrel* Barrel = nullptr;		
	UTankTurret* Turret = nullptr;

	UPROPERTY(EditDefaultsOnly, Category = "Firing")
	TSubclassOf<ATankProjectile> TankProjectile = nullptr;

	UPROPERTY(EditAnywhere, Category = "Firing")
	float FireSpeed = 50000;

	UPROPERTY(EditAnywhere, Category = "Firing")
	float ReloadTime = 3;
	
	float GameStart = 5;
	double LastFireTime = GameStart - ReloadTime;
};

I do not know what is the issue here either and I think this type of issue is happening for the second time now but I do not think I even dealt with it the first time and it just kind of went away.

I would also like to share that I have just brought the Unreal Multiplayer Master course over at udemy and I hope to create Battle Tank a multiplayer game that with real players and spawn AI Tanks when needed. I have also gotten a cool course called “Blender Game Vehicle Creation”, which looks to be promising. I hope to learn 3d modelling as I think it is really important when it comes to expressing your creativity and visions for the game. Hopefully, I can create some tanks of my own and other vehicle assets as well. This course has really been a eye opener. I started game dev about a year ago for a capstone project for grade 12. Back then, I did not know the best resources and did not have much time to devote. Lol I actually started with xcode, and then moved to unity. Unreal has definitely become my favorite engine and I am so excited for unreal engine 5. I am really passionate for game dev and a lot of that comes from this course so thank you guys.

What lecture are you on?

That shows the PlayerController class is TankPlayerController, you should be able to just click the drop down and search for the blueprint one, unless TankPlayerController is what you named the blueprint one.

Have you tried adding logs to see if the that AI controller is being used?

To get you started

float BestDistance = 1'000'000.f;
for (ATank* Tank : TActorRange<ATank>(GetWorld()))
{
    // determine best distance. Don't forget to exclude self.
}

Could you clarify? What’s different about the current behaviour?

Forget the details panel and use the blueprint construction script. You may need to expose more things to blueprint.

Also good luck with your goals! :slight_smile:

Hey,

I am still a little unclear about the TankPlayerController blueprint. Should the player controller class in the selected gamemode section be set as the tankplayercontroller blueprint or do we have to set it up somewhere else. In other levels such as the main menu and start menu, I still able to tank HUD (the crosshair) and I do not want that to happen.

I am currently on lecture 188: Adding Tick Component back. This is the lecture after the major refactoring that happens for the tank.cpp and the aiming component.

For the AI Tanks, I only want them to fire once they have locked on to their target tank and not fire in just 3s intervals because most of the time, they are firing at nothing and it does not look natural. Once the barrel is stationary for a certain time (say 0.5s) and pointing at a target, then I want the AI tank to fire.

I put some logs in the tank ai controller and I do not think that the ai controller is even setup properly. The tick component is not being called. I have also forgot where to set up the tank ai controller.

Maybe I’m misunderstanding what the issue is. What it sounds like is that you just want to click this


And change it to your blueprint one?

In the world settings use a game mode override and use Game Mode Base

Do the something similar as the player. Check if it’s “locked” on before allowing to fire.

What about BeginPlay?

In the Tank blueprint’s details.

No, I already have the TankPlayerController_BP set as the player controller class, you just can not see it in this view because the name is too long. I am just wondering if this is the right place to do it and if this is where the instructor did it. The instructor does not have the crosshair showing up in other levels such as the main menu so I thought that he must have set up the tankplayercontroller_BP somewhere else.

If I do have it in the right place, and I am supposed to use a game mode override, how do I do that and what do I do about the shaking of the turret and barrel?

In the world settings

image

Will look into the shaking tomorrow.

ok thanks
also how do i expose the tank projectile in the blueprint and why is this issue happening tho. it keeps resetting to none and hardcoded values in the code. It happened a while back with the turret static mesh. Everytime i compiled, the static mesh of the turret kept resetting to none.

For the shaking bug, here is my code for rotate and elevate functions in the turret and barrel class:

void UTankBarrel::Elevate(float RelativeSpeed)
{
    RelativeSpeed = FMath::Clamp<float>(RelativeSpeed, -1, 1);
    auto DeltaElevation = RelativeSpeed * MaxDegreesPerSecond * GetWorld()->DeltaTimeSeconds;
    auto Elevation = FMath::Clamp<float>(RelativeRotation.Pitch + DeltaElevation, MinElevationDegrees, MaxElevationDegrees);
    SetRelativeRotation(FRotator(Elevation, 0, 0));
}
void UTankTurret::Rotate(float RelativeSpeed)
{
    RelativeSpeed = FMath::Clamp<float>(RelativeSpeed, -1, 1);
    auto DeltaRotation = RelativeSpeed * MaxDegreesPerSecond * GetWorld()->DeltaTimeSeconds;
    auto Rotation = RelativeRotation.Yaw + DeltaRotation;
    SetRelativeRotation(FRotator(0, Rotation, 0));
}

Also, disregard the part about the AI tanks firing only when locked because I see that the instructor covers in a lecture.

What about the player controller? I suspect it’s due to the line trace hitting the barrel. You can add the actor to the ignored actors.

FCollisionQueryParams Params(NAME_None, false, GetPawn());

And then add that as the next argument to the line trace.

That was not the issue. I tried it and the ai tanks are also experiencing the same shaking.

If you want I could take a look at your project? Use File > Package Project > Zip Up Project and upload that somewhere.

where should I upload it?
It is about 5gb.

I have one more question. Instead of changing the crosshair to a different color when I reload, I want to play an animation I have set up. The animation duration is the same as the reload time but that is hard coded. How do i make it so that the animation plays once everytime the tank reloads?

Anywhere sensible, Google Drive, Dropbox, etc.

Even using the method described above? File > Package Project > Zip? Because that only includes needed files and excludes things like Binaries and other generated/compiled files.

Well you could bind an event, so when the animation finishes playing it calls “EnableFire” or something like that.

Yes, i did zip the package project. I have uploaded on to google drive. What is your email so I can share it with you?

dan@gamedev.tv

or just PM the link.

Did it work?

I’m not experiencing any shaking on my end.

What engine version are you using?

Also, could you try assigning the landscape material instance that I have created in the folder surfaces to the landscape? I wanna see if you get the same shadow effect.

4.25

It is indeed dark and comes with a black line of darkness when you get close.

Privacy & Terms