Debugging Fire / Added Turning / Added rotation to tank and turrets

This commit is contained in:
2025-07-20 07:44:04 -04:00
parent aee1f481cb
commit 0c58992652
11 changed files with 185 additions and 42 deletions

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@@ -1,8 +1,9 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "Components/CapsuleComponent.h"
#include "BasePawn.h"
#include "Components/CapsuleComponent.h"
#include "Kismet/GameplayStatics.h"
// Sets default values
ABasePawn::ABasePawn()
@@ -24,20 +25,40 @@ ABasePawn::ABasePawn()
}
// Called when the game starts or when spawned
void ABasePawn::BeginPlay()
void ABasePawn::RotateTurret(FVector LookAtTarget)
{
Super::BeginPlay();
FVector ToTarget = LookAtTarget - TurretMesh->GetComponentLocation();
FRotator LookAtRotation = FRotator(0.f, ToTarget.Rotation().Yaw, 0.f);
TurretMesh->SetWorldRotation(
FMath::RInterpTo(
TurretMesh->GetComponentRotation(),
LookAtRotation,
UGameplayStatics::GetWorldDeltaSeconds(this),
25.f));
}
void ABasePawn::Fire()
{
FVector ProjectileSpawnPointLocation = ProjectileSpawnPoint->GetComponentLocation();
DrawDebugSphere(
GetWorld(),
ProjectileSpawnPointLocation,
25.f,
12,
FColor::Red,
false,
3.f);
}
// Called every frame
void ABasePawn::Tick(float DeltaTime)
/*void ABasePawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
/*FVector DeltaLocation(0.f);
DeltaLocation.X = 2.f;
AddActorLocalOffset(DeltaLocation);*/
AddActorLocalOffset(DeltaLocation);
}
}*/

View File

@@ -31,10 +31,10 @@ public:
UPROPERTY(EditInstanceOnly)
int32 EditInstanceOnlyInt = 14;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
void RotateTurret(FVector LookAtTarget);
void Fire();
private:
@@ -57,8 +57,4 @@ private:
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Super Duper Variables", meta = (AllowPrivateAccess = "true"))
int32 VisibleAnywhere = 12;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};

View File

@@ -6,6 +6,7 @@
#include "Camera/CameraComponent.h"
#include "Kismet/GameplayStatics.h"
#include "Components/InputComponent.h"
#include "DrawDebugHelpers.h"
ATank::ATank()
{
@@ -26,9 +27,54 @@ void ATank::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &ATank::Move);
PlayerInputComponent->BindAxis(TEXT("Turn"), this, &ATank::Turn);
PlayerInputComponent->BindAction(TEXT("Fire"), IE_Pressed, this, &ATank::Fire);
}
void ATank::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (PlayerControllerRef)
{
FHitResult HitResult;
PlayerControllerRef->GetHitResultUnderCursor(
ECollisionChannel::ECC_Visibility,
false,
HitResult);
RotateTurret(HitResult.ImpactPoint);
/*DrawDebugSphere(
GetWorld(),
HitResult.ImpactPoint,
25.f,
12,
FColor::Red,
false,
-1.f);*/
}
}
// Called when the game starts or when spawned
void ATank::BeginPlay()
{
Super::BeginPlay();
PlayerControllerRef = Cast<APlayerController>(GetController());
/*
DrawDebugSphere(
GetWorld(),
GetActorLocation() + FVector(0.f, 0.f, 200.f),
100.f,
12,
FColor::Red,
true,
30.f);
*/
}
void ATank::Move(float Value)
{
//FVector DeltaLocation(0.f); //Same as line below
@@ -42,5 +88,8 @@ void ATank::Move(float Value)
void ATank::Turn(float TValue)
{
//UE_LOG(LogTemp, Warning, TEXT("Value: %f"), TValue);
FRotator DeltaRotation = FRotator::ZeroRotator;
// Yaw = TValue * DeltaTime * TurnRate;
DeltaRotation.Yaw = TValue * TurnRate * UGameplayStatics::GetWorldDeltaSeconds(this);
AddActorLocalRotation(DeltaRotation, true);
}

View File

@@ -20,6 +20,13 @@ public:
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
// Called every frame
virtual void Tick(float DeltaTime) override;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
UPROPERTY(VisibleAnywhere, Category = "Components")
class USpringArmComponent* SpringArm;
@@ -30,7 +37,11 @@ private:
UPROPERTY(EditAnywhere, Category = "Movement")
float Speed = 200.f;
void Move(float Value);
UPROPERTY(EditAnywhere, Category = "Movement")
float TurnRate = 135.f;
void Move(float Value);
void Turn(float TValue);
APlayerController* PlayerControllerRef;
};

View File

@@ -0,0 +1,32 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "Tower.h"
#include "Tank.h"
#include "Kismet/GameplayStatics.h"
void ATower::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// Find the distance to the tank
if (Tank)
{
float Distance = FVector::Dist(GetActorLocation(), Tank->GetActorLocation());
// check to see if the tank is in range
if (Distance <= FireRange)
{
// if in range, rotate turret toward the tank
RotateTurret(Tank->GetActorLocation());
}
}
}
void ATower::BeginPlay()
{
Super::BeginPlay();
Tank = Cast<ATank>(UGameplayStatics::GetPlayerPawn(this, 0));
}

32
Source/ToonTanks/Tower.h Normal file
View File

@@ -0,0 +1,32 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "BasePawn.h"
#include "Tower.generated.h"
/**
*
*/
UCLASS()
class TOONTANKS_API ATower : public ABasePawn
{
GENERATED_BODY()
public:
virtual void Tick(float DeltaTime) override;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
class ATank* Tank;
UPROPERTY(EditDefaultsOnly, Category="Combat")
float FireRange = 700.f;
};

View File

@@ -16,11 +16,8 @@
"Enabled": true,
"SupportedTargetPlatforms": [
"Win64"
],
"MarketplaceURL": "com.epicgames.launcher://ue/marketplace/product/362651520df94e4fa65492dbcba44ae2"
}
]
}
],
"TargetPlatforms": [],
"AdditionalRootDirectories": [],
"AdditionalPluginDirectories": [],
"EpicSampleNameHash": ""
}