Problem:- After setting the BP_GameModeBaseClass my tank is spawning in the sky rather than spawning on the ground.
NOTE:- Everything was fine before setting the game mode class.
BP_GameModeBaseClass:-
BP_PawnTank:-
I don’t think that the c++ code of the tank will help solve this problem.
But Still here is the code in case the problem is related to the code.
BasePawn Header:-
`// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include “CoreMinimal.h”
#include “GameFramework/Pawn.h”
#include “BasePawn.generated.h”
UCLASS()
class TOONTANKS_API ABasePawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
ABasePawn();
protected:
void RotateTurret(FVector LookAtTarget);
void FireProjectile();
private:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
class UCapsuleComponent* CapsuleComp = nullptr;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
UStaticMeshComponent* BaseMesh = nullptr;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
UStaticMeshComponent* Turret = nullptr;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
USceneComponent* ProjectileSpawnPoint = nullptr;
UPROPERTY(EditDefaultsOnly, Category = "Combat")
TSubclassOf<class AProjectile> ProjectileClass;
};`
BasePawn CPP-
`// Fill out your copyright notice in the Description page of Project Settings.
#include “BasePawn.h”
#include “Components/CapsuleComponent.h”
#include “Projectile.h”
// Sets default values
ABasePawn::ABasePawn()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
CapsuleComp = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Capsule Collider")); //Storing Adddress of UCapsuleComponent Subobject in CapsuleComp
RootComponent = CapsuleComp; //*Assigning CapsuleComp as the RootComponent of the BasePawn
BaseMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Base Mesh"));
BaseMesh->SetupAttachment(CapsuleComp); //*Setting Up the parent component for following transformation
Turret = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Turret"));
Turret->SetupAttachment(BaseMesh);
ProjectileSpawnPoint = CreateDefaultSubobject<USceneComponent>(TEXT("Projectile Spawn Point"));
ProjectileSpawnPoint->SetupAttachment(Turret);
}
void ABasePawn::RotateTurret(FVector LookAtTarget)
{
FVector ToTarget = LookAtTarget - Turret->GetComponentLocation();
FRotator TurretRotation = FRotator(0.f, ToTarget.Rotation().Yaw, 0.f);
Turret->SetWorldRotation(TurretRotation);
}
void ABasePawn::FireProjectile()
{
FVector ProjectileLocation = ProjectileSpawnPoint->GetComponentLocation();
FRotator ProjectileRotation = ProjectileSpawnPoint->GetComponentRotation();
auto Projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileClass, ProjectileLocation, ProjectileRotation);
Projectile->SetOwner(this);
}`
**Tank.h - **
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "BasePawn.h"
#include "Tank.generated.h"
/**
*
*/
UCLASS()
class TOONTANKS_API ATank : public ABasePawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
ATank();
// 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* SpringArmComp = nullptr;
UPROPERTY(VisibleAnywhere, Category = "Components")
class UCameraComponent* CameraComp = nullptr;
UPROPERTY(EditAnywhere, Category = "Movement")
float Speed = 200.0f;
void Move(float Value);
void Turn(float Value);
APlayerController* PlayerControllerRef = nullptr;
};
Tank.CPP:-
// Fill out your copyright notice in the Description page of Project Settings.
#include "Tank.h"
#include "Camera/CameraComponent.h"
#include "DrawDebugHelpers.h"
#include "GameFramework/SpringArmComponent.h"
#include "Kismet/GameplayStatics.h"
#define OUT
ATank::ATank()
{
SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("Spring Arm"));
SpringArmComp->SetupAttachment(RootComponent);
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
CameraComp->SetupAttachment(SpringArmComp);
}
// Called to bind functionality to input
void ATank::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &ATank::Move);
PlayerInputComponent->BindAxis(TEXT("Turn"), this, &ATank::Turn);
PlayerInputComponent->BindAction(TEXT("Fire"), IE_Pressed, this, &ATank::FireProjectile);
}
void ATank::BeginPlay()
{
Super::BeginPlay();
PlayerControllerRef = Cast<APlayerController>(GetController());
}
void ATank::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (PlayerControllerRef)
{
FHitResult HitResult;
PlayerControllerRef->GetHitResultUnderCursor(ECollisionChannel::ECC_Visibility, false, OUT HitResult);
DrawDebugSphere(GetWorld(), HitResult.ImpactPoint, 10.f, 8, FColor::Red);
RotateTurret(HitResult.ImpactPoint);
}
}
void ATank::Move(float Value)
{
FVector MoveOffset = FVector::ZeroVector;
MoveOffset.X = Value * Speed * UGameplayStatics::GetWorldDeltaSeconds(this);
AddActorLocalOffset(MoveOffset, true);
}
void ATank::Turn(float Value)
{
FRotator TurnOffset = FRotator::ZeroRotator;
TurnOffset.Yaw = Value * (Speed/3.5f) * UGameplayStatics::GetWorldDeltaSeconds(this);
AddActorLocalRotation(TurnOffset, true);
}