Collision Issue

Hello there, I have some unknown issue with the collision of my tank pawn, for some reason it doesn’t collide neither with walls nor with turrets. If I play the game with the default pawn I collide with walls and turrets but not with the tank, so I suppose that the problem is in it but I have not been able to identify it, I need help…

Basepawn.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "BasePawn.generated.h"

class UCapsuleComponent;

UCLASS()
class TOONTANKSREBORN_API ABasePawn : public APawn
{
	GENERATED_BODY()

	public:

		// Constructor
		ABasePawn();

		// Called every frame
		virtual void Tick(float DeltaTime) override;

		// Called to bind functionality to input
		virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

	protected:
		// Called when the game starts or when spawned
		virtual void BeginPlay() override;

		UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Components", meta=(AllowPrivateAccess="true"))
			UStaticMeshComponent* TurretMesh = nullptr;

	private:

		UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Components", meta=(AllowPrivateAccess="true"))
			USceneComponent* ActorOrigin = nullptr;

		UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Components", meta=(AllowPrivateAccess="true"))
			UCapsuleComponent* CapsuleComponent = nullptr;

		UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Components", meta=(AllowPrivateAccess="true"))
			UStaticMeshComponent* BaseMesh = nullptr;

		UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Components", meta=(AllowPrivateAccess="true"))
			USceneComponent* ProjectileSpawnPoint = nullptr;
};

BasePawn.cpp

#include "TankPawn.h"
#include "Camera/CameraComponent.h"
#include "Components/StaticMeshComponent.h"
#include "GameFramework/SpringArmComponent.h"

// Sets default values
ATankPawn::ATankPawn()
{
	PrimaryActorTick.bCanEverTick = true;

	RightGun = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Right Gun"));
	RightGun->SetupAttachment(TurretMesh);

	LeftGun = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Left Gun"));
	LeftGun->SetupAttachment(TurretMesh);

	SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("Spring Arm"));
	SpringArm->SetupAttachment(RootComponent);
		
	Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
	Camera->SetupAttachment(SpringArm);
}

// Called when the game starts or when spawned
void ATankPawn::BeginPlay()
{
	Super::BeginPlay();
}

// Called every frame
void ATankPawn::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	Rotate();
	Move();
}

// Called to bind functionality to input
void ATankPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	PlayerInputComponent->BindAxis("MoveForward", this, &ATankPawn::CalculateMovementInput);
	PlayerInputComponent->BindAxis("Turn", this, &ATankPawn::CalculateRotationInput);
}

void ATankPawn::CalculateMovementInput(float Value)
{
	const float MovementAmount = Value * MovementSpeed * GetWorld()->DeltaTimeSeconds;
	MovementDirection = FVector(0.0f, MovementAmount, 0.0f);
}

void ATankPawn::CalculateRotationInput(float Value)
{
	const float RotationAmount = Value * RotationSpeed * GetWorld()->DeltaTimeSeconds;
	const FRotator ActualRotation = {0.0f, RotationAmount, 0.0f};
	
	RotationDirection = FQuat(ActualRotation);
}

void ATankPawn::Move()
{
	AddActorLocalOffset(MovementDirection, true);
}

void ATankPawn::Rotate()
{
	AddActorLocalRotation(RotationDirection, true);
}

Tankpawn.h

#pragma once

#include "BasePawn.h"
#include "CoreMinimal.h"
#include "TankPawn.generated.h"

class UCameraComponent;
class USpringArmComponent;

UCLASS()
class TOONTANKSREBORN_API ATankPawn : public ABasePawn
{
	GENERATED_BODY()

	public:
	
		ATankPawn();
	
    	virtual void Tick(float) override;
    	virtual void SetupPlayerInputComponent(class UInputComponent*) override;

	protected:
	
		virtual void BeginPlay() override;

	private:

		UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category="Components", meta=(AllowPrivateAccess="true"))
            USpringArmComponent* SpringArm = nullptr;
                
        UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category="Components", meta=(AllowPrivateAccess="true"))
            UCameraComponent* Camera = nullptr;

		UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Components", meta=(AllowPrivateAccess="true"))
			UStaticMeshComponent* RightGun = nullptr;

		UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Components", meta=(AllowPrivateAccess="true"))
			UStaticMeshComponent* LeftGun = nullptr;

		UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Properties", meta=(AllowPrivateAccess="true"))
			float MovementSpeed = 300.0f;

		UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Properties", meta=(AllowPrivateAccess="true"))
			float RotationSpeed = 150.0f;

		FVector MovementDirection;
		FQuat RotationDirection;

		void CalculateMovementInput(float);
		void CalculateRotationInput(float);

		void Move();
		void Rotate();
};

TankPawn.cpp

#include "TankPawn.h"
#include "Camera/CameraComponent.h"
#include "Components/StaticMeshComponent.h"
#include "GameFramework/SpringArmComponent.h"

// Sets default values
ATankPawn::ATankPawn()
{
	PrimaryActorTick.bCanEverTick = true;

	RightGun = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Right Gun"));
	RightGun->SetupAttachment(TurretMesh);

	LeftGun = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Left Gun"));
	LeftGun->SetupAttachment(TurretMesh);

	SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("Spring Arm"));
	SpringArm->SetupAttachment(RootComponent);
		
	Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
	Camera->SetupAttachment(SpringArm);
}

// Called when the game starts or when spawned
void ATankPawn::BeginPlay()
{
	Super::BeginPlay();
}

// Called every frame
void ATankPawn::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	Rotate();
	Move();
}

// Called to bind functionality to input
void ATankPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	PlayerInputComponent->BindAxis("MoveForward", this, &ATankPawn::CalculateMovementInput);
	PlayerInputComponent->BindAxis("Turn", this, &ATankPawn::CalculateRotationInput);
}

void ATankPawn::CalculateMovementInput(float Value)
{
	const float MovementAmount = Value * MovementSpeed * GetWorld()->DeltaTimeSeconds;
	MovementDirection = FVector(0.0f, MovementAmount, 0.0f);
}

void ATankPawn::CalculateRotationInput(float Value)
{
	const float RotationAmount = Value * RotationSpeed * GetWorld()->DeltaTimeSeconds;
	const FRotator ActualRotation = {0.0f, RotationAmount, 0.0f};
	
	RotationDirection = FQuat(ActualRotation);
}

void ATankPawn::Move()
{
	AddActorLocalOffset(MovementDirection, true);
}

void ATankPawn::Rotate()
{
	AddActorLocalRotation(RotationDirection, true);
}

This should be solved later.

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

Privacy & Terms