#include "GameFramework/Actor.h" class AObstacle : public AActor { GENERATED_BODY() public: AObstacle() { // Set the position and collision properties SetActorLocation(FVector(0.f, 0.f, 100.f)); SetActorScale3D(FVector(1.f, 1.f, 2.f)); SetActorEnableCollision(true); } };
#include "GameFramework/Actor.h" #include "MyProjectile.generated.h" UCLASS() class AMyProjectile : public AActor { GENERATED_BODY() public: AMyProjectile() { // Set the movement speed and initialize damage MovementSpeed = 1000.f; Damage = 10.f; // Set up the collision detection GetCollisionComponent()->OnComponentBeginOverlap.AddDynamic(this, &AMyProjectile::OnOverlapBegin); // Set up the visual representation Mesh = CreateDefaultSubobjectThe AActor class is part of the GameFramework package of the Unreal Engine API, which provides classes and functions for implementing game-specific logic and interaction.(TEXT("Mesh")); static ConstructorHelpers::FObjectFinder SphereMesh(TEXT("/Engine/BasicShapes/Sphere")); Mesh->SetStaticMesh(SphereMesh.Object); RootComponent = Mesh; } virtual void Tick(float DeltaTime) override { // Move the projectile forward FVector Location = GetActorLocation(); Location += GetActorForwardVector() * MovementSpeed * DeltaTime; SetActorLocation(Location); } void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) { // If we hit something that can take damage, deal damage and destroy the projectile AActor* OwningActor = GetOwner(); if (OwningActor && OwningActor != OtherActor && OtherActor->GetClass()->ImplementsInterface(UVictimInterface::StaticClass())) { IVictimInterface::Execute_ApplyDamage(OtherActor, Damage); Destroy(); } } private: UPROPERTY(EditDefaultsOnly, Category = Projectile) float MovementSpeed; UPROPERTY(EditDefaultsOnly, Category = Projectile) float Damage; UPROPERTY(EditDefaultsOnly, Category = Projectile) UStaticMeshComponent* Mesh; };