Help. … me teacher.Projectile is like that and onhit destroy is not make.
And … So sorry for my english skill. I am not Nagtive English language and can not talk to more than normal state. So Sorry teacher.
Could you post your code please?
ProjectileBasse.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "ToonTanks/Actors/ProjectileBasse.h"
#include "Components/StaticMeshComponent.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Kismet/GameplayStatics.h"
// Sets default values
AProjectileBasse::AProjectileBasse()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
ProjecitleMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Projectile Mesh"));
ProjecitleMesh->OnComponentHit.AddDynamic(this, &AProjectileBasse::OnHit);
RootComponent = ProjecitleMesh;
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("Projectile Movement"));
ProjectileMovement->InitialSpeed = MovementSpeed;
ProjectileMovement->MaxSpeed = MovementSpeed;
InitialLifeSpan = 3.0f;
}
// Called when the game starts or when spawned
void AProjectileBasse::BeginPlay()
{
Super::BeginPlay();
}
void AProjectileBasse::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
AActor* MyOwner = GetOwner();
if (!MyOwner)
{
return;
}
// If the other ISN'T self OR Owner AND exists, then apply damage.
if (OtherActor && OtherActor != this && OtherActor != MyOwner)
{
UGameplayStatics::ApplyDamage(OtherActor, Damage, MyOwner->GetInstigatorController(), this, DamageType);
}
// Play a bunch of effects here during the polish phase. - TODO
Destroy();
}
ProjectileBasse.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ProjectileBasse.generated.h"
class UProjectileMovementComponent;
UCLASS()
class TOONTANKS_API AProjectileBasse : public AActor
{
GENERATED_BODY()
private:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
UProjectileMovementComponent* ProjectileMovement;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
UStaticMeshComponent* ProjecitleMesh;
UPROPERTY(EditDefaultsOnly, Category = "Damage")
TSubclassOf<UDamageType> DamageType;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Movement", meta = (AllowPrivateAccess = "true"))
float MovementSpeed = 1300;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Damage", meta = (AllowPrivateAccess = "true"))
float Damage = 50;
UFUNCTION()
void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
public:
// Sets default values for this actor's properties
AProjectileBasse();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
};
PawnTurrent.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "PawnTurrent.h"
#include "Kismet/GameplayStatics.h"
#include "PawnTank.h"
// Called when the game starts or when spawned
void APawnTurrent::BeginPlay()
{
Super::BeginPlay();
GetWorld()->GetTimerManager().SetTimer(FireRateTimerHandle, this, &APawnTurrent::CheckFireCondition, FireRate, true);
PlayerPawn = Cast<APawnTank>(UGameplayStatics::GetPlayerPawn(this, 0));
}
void APawnTurrent::HandleDestruction()
{
Super::HandleDestruction();
Destroy();
}
// Called every frame
void APawnTurrent::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (!PlayerPawn || ReturnDistanceToPlayer() > FireRange)
{
return;
}
RotateTurrent(PlayerPawn->GetActorLocation());
}
void APawnTurrent::CheckFireCondition()
{
//if Player == null || is Dead THEN BAIL!!
if (!PlayerPawn)
{
return;
}
//If Player IS in range THEN FIRE!!
if (ReturnDistanceToPlayer() <= FireRange)
{
// Fire
UE_LOG(LogTemp, Warning, TEXT("Fire Condition Checked"));
Fire();
}
}
float APawnTurrent::ReturnDistanceToPlayer()
{
if (!PlayerPawn)
{
return 0.0f;
}
return FVector::Dist(PlayerPawn->GetActorLocation(), GetActorLocation());
}
PawnTurrent.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "PawnBase.h"
#include "PawnTurrent.generated.h"
class APawnTank;
UCLASS()
class TOONTANKS_API APawnTurrent : public APawnBase
{
GENERATED_BODY()
private:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Combat", meta = (AllowPrivateAccess = "true"))
float FireRate = 2.0f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Combat", meta = (AllowPrivateAccess = "true"))
float FireRange = 500.0f;
FTimerHandle FireRateTimerHandle;
APawnTank* PlayerPawn;
void CheckFireCondition();
float ReturnDistanceToPlayer();
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
virtual void HandleDestruction() override;
};
PawnTank.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "PawnTank.h"
#include "GameFrameWork/SpringArmComponent.h"
#include "GameFramework/Actor.h"
#include "Camera/CameraComponent.h"
// Called when the game starts or when spawned
void APawnTank::BeginPlay()
{
Super::BeginPlay();
PlayerControllerRef = Cast<APlayerController>(GetController());
}
void APawnTank::HandleDestruction()
{
Super::HandleDestruction();
// Hide Player . TODO - Create New function to handle this.
}
// Called every frame
void APawnTank::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
Rotate();
Move();
if (PlayerControllerRef)
{
FHitResult TraceHitResult;
PlayerControllerRef->GetHitResultUnderCursor(ECC_Visibility, false, TraceHitResult);
FVector HitLocation = TraceHitResult.ImpactPoint;
RotateTurrent(HitLocation);
}
}
// Called to bind functionality to input
void APawnTank::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &APawnTank::CalculateMoveInput);
PlayerInputComponent->BindAxis("Turn", this, &APawnTank::CalculateRotateInput);
PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &APawnTank::Fire);
}
void APawnTank::CalculateMoveInput(float Value)
{
MoveDirection = FVector( Value * MoveSpeed * GetWorld()->DeltaTimeSeconds, 0 ,0);
}
void APawnTank::CalculateRotateInput(float Value)
{
float RotateAmount = Value * RotateSpeed * GetWorld()->DeltaTimeSeconds;
FRotator Rotation = FRotator(0, RotateAmount ,0);
RotationDirection = FQuat(Rotation);
}
void APawnTank::Move()
{
AddActorLocalOffset(MoveDirection, true);
}
void APawnTank::Rotate()
{
AddActorLocalRotation(RotationDirection, true);
}
APawnTank::APawnTank()
{
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("Spring Arm"));
SpringArm->SetupAttachment(RootComponent);
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
Camera->SetupAttachment(SpringArm);
}
PawnTank.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "PawnBase.h"
#include "PawnTank.generated.h"
class USpringArmComponent;
class UCameraComponent;
UCLASS()
class TOONTANKS_API APawnTank : public APawnBase
{
GENERATED_BODY()
private:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
USpringArmComponent* SpringArm;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
UCameraComponent* Camera;
FVector MoveDirection;
FQuat RotationDirection;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement", meta =(AllowPrivateAccess = "true"))
float MoveSpeed = 100.0f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement", meta =(AllowPrivateAccess = "true"))
float RotateSpeed = 100.0f;
APlayerController* PlayerControllerRef;
void CalculateMoveInput(float Value);
void CalculateRotateInput(float Value);
void Move();
void Rotate();
public:
APawnTank();
// 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;
virtual void HandleDestruction() override;
};
PawnBase.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "PawnBase.h"
#include "Components/CapsuleComponent.h"
#include "ToonTanks/Actors/ProjectileBasse.h"
// Sets default values
APawnBase::APawnBase()
{
// 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"));
RootComponent = CapsuleComp;
BaseMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Base Mesh"));
BaseMesh->SetupAttachment(RootComponent);
TurretMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Turrent Mesh"));
TurretMesh->SetupAttachment(BaseMesh);
ProjectileSpawnPoint = CreateDefaultSubobject<USceneComponent>(TEXT("Projectile Spawn Point"));
ProjectileSpawnPoint->SetupAttachment(TurretMesh);
}
void APawnBase::RotateTurrent(FVector LookAtTarget)
{
// Update TurretMesh rotation to face towards the LookAtTarget passed in from Child Classes.
// TurretMesh->SetWorldRotation()...
FVector LookAtTargetCleaned = FVector(LookAtTarget.X, LookAtTarget.Y, TurretMesh->GetComponentLocation().Z);
FVector StartLocation = FVector(TurretMesh->GetComponentLocation());
FRotator TurretRotation = FVector( LookAtTargetCleaned - StartLocation ).Rotation();
TurretMesh->SetWorldRotation(TurretRotation);
}
void APawnBase::Fire()
{
// Get ProjectileSpawnPoint Location && Rotation -> Spawn Projectile class
// at Location firing towards Rotation.
if (ProjectileClass)
{
FVector SpawnLocation = ProjectileSpawnPoint->GetComponentLocation();
FRotator SpawnRotation = ProjectileSpawnPoint->GetComponentRotation();
AProjectileBasse* TempProjectile = GetWorld()->SpawnActor<AProjectileBasse>(ProjectileClass, SpawnLocation, SpawnRotation);
TempProjectile->SetOwner(this);
}
}
void APawnBase::HandleDestruction()
{
// --- Universal functionality ---
// Play death effects particle, sound and camera shake.
// --- Then do Child overrides ---
// -- PawnTurret - Inform GameMode Turret died -> Then Destroy() self.
// -- PawnTank - Inform GameMode Player died -> Then Hide() all components && stop movement input.
}
PawnBase.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "PawnBase.generated.h"
class UCapsuleComponent;
class AProjectileBasse;
UCLASS()
class TOONTANKS_API APawnBase : public APawn
{
GENERATED_BODY()
private:
// COMPONENTS
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
UCapsuleComponent* CapsuleComp;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
UStaticMeshComponent* BaseMesh;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
UStaticMeshComponent* TurretMesh;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
USceneComponent* ProjectileSpawnPoint;
// VARIABLES
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Projectile Type", meta = (AllowPrivateAccess = "true"))
//TSubclassOf<AProjectileBasse> ProjectileBase;
TSubclassOf<AProjectileBasse> ProjectileClass;
public:
// Sets default values for this pawn's properties
APawnBase();
protected:
void RotateTurrent(FVector LookAtTarget);
void Fire();
virtual void HandleDestruction();
};
This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.