You’ve selected to upload a folder that’s why. You need to select to upload a file.
Okay! It’s all set.
https://drive.google.com/drive/folders/1KO89RnzwxaTjNjsydX3iG2dXyr9j_oN2
…And Public now.
You didn’t do what I had suggested in the screenshot above.
- SetTankReference is the function that we added that sets the variable in code.
SetStaticMesh does exactly what it says, they’re different things. - You have commented out AimTowardsCrosshair.
Okay, I got the putting the turret on part, but it’s now time for the second problem.
We still have the floating situation and the fact that it can’t aim. What to do about that?
I forgot to mention that you need to sort out the collision with your meshes. Ben removed his barrel and turret collision.
As for aiming, did you uncomment that line? That’s all I did and it works fine, though your line trace is a bit short so can’t reach that far.
I did remove the comments, but I might as well so you my code now.
(TankPlayerController.cpp)
// Fill out your copyright notice in the Description page of Project Settings.
#include "Tank.h"
#include "TankPlayerController.h"
void ATankPlayerController::BeginPlay()
{
Super::BeginPlay();
auto ControlledTank = GetControlledTank();
if (!ControlledTank) {
UE_LOG(LogTemp, Warning, TEXT("PlayerController not possessing a tank"));
}
else
{
UE_LOG(LogTemp, Warning, TEXT("PlayerController possessing: %s"), *(ControlledTank->GetName()));
}
}
void ATankPlayerController::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
UE_LOG(LogTemp, Warning, TEXT("Player controller ticking"));
}
ATank* ATankPlayerController::GetControlledTank() const {
return Cast<ATank>(GetPawn());
}
void ATankPlayerController::AimTowardsCrosshair()
{
if (!GetControlledTank()) { return; }
FVector HitLocation;
if (GetSightRayHitLocation(HitLocation))
{
GetControlledTank()->AimAt(HitLocation);
};
};
bool ATankPlayerController::GetSightRayHitLocation(FVector& HitLocation) const
{
int32 ViewportSizeX, ViewportSizeY;
GetViewportSize(ViewportSizeX, ViewportSizeY);
auto ScreenLocation = FVector2D(ViewportSizeX * CrosshairXLocation, ViewportSizeY * CrosshairYLocation);
UE_LOG(LogTemp, Warning, TEXT("ScreenLocation: %s"), *ScreenLocation.ToString());
FVector LookDirection;
if (GetLookDirection(ScreenLocation, LookDirection))
{
UE_LOG(LogTemp, Warning, TEXT("LookDirection: %s"), *LookDirection.ToString());
GetLookVectorHitLocation(LookDirection, HitLocation);
}
return true;
}
bool ATankPlayerController::GetLookVectorHitLocation(FVector LookDirection, FVector& HitLocation) const
{
FHitResult HitResult;
auto StartLocation = PlayerCameraManager->GetCameraLocation();
auto EndLocation = StartLocation + (LookDirection * LineTraceRange);
if (GetWorld()->LineTraceSingleByChannel(
HitResult,
StartLocation,
EndLocation,
ECollisionChannel::ECC_Visibility))
{
HitLocation = HitResult.Location;
return true;
}
HitLocation = FVector(0);
return false;
}
bool ATankPlayerController::GetLookDirection(FVector2D ScreenLocation, FVector& LookDirection) const
{
FVector CameraWorldLocation;
return DeprojectScreenPositionToWorld(
ScreenLocation.X,
ScreenLocation.Y,
CameraWorldLocation,
LookDirection);
};
AimTowardsCrosshair
is what does the aiming. I said uncomment not delete the whole line
void ATankPlayerController::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
AimTowardsCrosshair();
UE_LOG(LogTemp, Warning, TEXT("Player controller ticking"));
}
So, what about the fact that my tank won’t stop floating? That’s the next, if not the last problem.
What floating? Did you remove the collision from the tank and barrel?
I’ll show you what I mean in the video of my Google Drive, since this site doesn’t accept things that are 10,000 MB either.
https://drive.google.com/drive/folders/1KO89RnzwxaTjNjsydX3iG2dXyr9j_oN2
Yup, definitely looks like you didn’t remove their collision.
Remove not disable. Open the static mesh editor and then go to collision > remove collision.
The tank is floating again!
Could you show me what you have done?
As you can see here, I removed the collision…
And as you can see here I set the Tank back to PhysicsActor.
What meshes specifically?
tank_fbx_Body_Hull_V
Sorry I meant to write turret and barrel earlier
Okay, phew! Got that done… But we’re still in the situation where the tank can’t aim. I.e. the turret and barrel aren’t moving.