// Copyright Zelle Games #include "BasicProjectile.h" #include "Components/SphereComponent.h" #include "Engine/DamageEvents.h" #include "GameFramework/ProjectileMovementComponent.h" // Sets default values ABasicProjectile::ABasicProjectile() { // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = true; SphereComponent = CreateDefaultSubobject(TEXT("Sphere Collision")); SphereComponent->InitSphereRadius(50.0f); RootComponent = SphereComponent; SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &ABasicProjectile::OnSphereOverlap); SphereComponent->SetCollisionProfileName("NoCollision"); MeshComponent = CreateDefaultSubobject(TEXT("Mesh")); MeshComponent->SetCollisionProfileName("NoCollision"); MeshComponent->SetupAttachment(RootComponent); ProjectileMovementComponent = CreateDefaultSubobject(TEXT("Projectile Movement")); ProjectileMovementComponent->SetUpdatedComponent(RootComponent); ProjectileMovementComponent->ProjectileGravityScale = 0; } // Called when the game starts or when spawned void ABasicProjectile::BeginPlay() { Super::BeginPlay(); } // Called every frame void ABasicProjectile::Tick(float DeltaTime) { Super::Tick(DeltaTime); } void ABasicProjectile::OnSphereOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) { FVector UnitDirection = OtherActor->GetActorLocation() - GetActorLocation(); UnitDirection.Normalize(); FPointDamageEvent DamageEvent; DamageEvent.HitInfo.ImpactPoint = SweepResult.ImpactPoint; DamageEvent.ShotDirection = UnitDirection; DamageEvent.Damage = Damage; OtherActor->TakeDamage(Damage, DamageEvent, OwningController, this); Destroy(); }