diff --git a/Source/M4_CPP/private/M4_PlayerPawn.cpp b/Source/M4_CPP/private/M4_PlayerPawn.cpp index 6c80f23..4294b63 100644 --- a/Source/M4_CPP/private/M4_PlayerPawn.cpp +++ b/Source/M4_CPP/private/M4_PlayerPawn.cpp @@ -1,7 +1,9 @@ #include "M4_PlayerPawn.h" #include "EnhancedInputComponent.h" #include "M4_Gamemode.h" +#include "M4_Projectile.h" #include "GameFramework/PlayerController.h" +#include "Kismet/KismetMathLibrary.h" #include "Camera/CameraComponent.h" AM4_PlayerPawn::AM4_PlayerPawn() @@ -87,6 +89,19 @@ void AM4_PlayerPawn::Shoot(const FInputActionInstance& Inst) { if (Inst.GetValue().Get() == true) { - GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("Input Space Pressed")); + GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Using the Wand!")); + UWorld* const World = GetWorld(); + if (World !=nullptr && Proj != nullptr) + { + FVector TargetPosition = FVector3d(GetActorLocation().X, GetActorLocation().Y, GetActorLocation().Z); + + FRotator SpawnRotation = UKismetMathLibrary::FindLookAtRotation(FVector3d(0), TargetPosition); + + FActorSpawnParameters ActorSpawnParams; + ActorSpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding; + + //World->SpawnActor(Proj, TargetPosition, SpawnRotation, ActorSpawnParams); + } + } }; diff --git a/Source/M4_CPP/private/M4_Projectile.cpp b/Source/M4_CPP/private/M4_Projectile.cpp index e481d10..8f8efb4 100644 --- a/Source/M4_CPP/private/M4_Projectile.cpp +++ b/Source/M4_CPP/private/M4_Projectile.cpp @@ -12,24 +12,14 @@ AM4_Projectile::AM4_Projectile() MeshComp-> SetRelativeScale3D(FVector(1.f, .1f,.1f)); MeshComp->BodyInstance.SetCollisionProfileName(TEXT("Projectile")); MeshComp->OnComponentHit.AddDynamic(this, &AM4_Projectile::OnHit); + + MeshComp->SetupAttachment(RootComponent); + MeshComp->SetMobility(EComponentMobility::Movable); + MeshComp->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); } void AM4_Projectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector Impulse, const FHitResult& Hit) { - -} - - -/* Called when the game starts or when spawned -void AM4_Projectile::BeginPlay() -{ - Super::BeginPlay(); - -} - -Called every frame -void AM4_Projectile::Tick(float DeltaTime) -{ - Super::Tick(DeltaTime); -}*/ - + OtherComp->AddImpulseAtLocation(GetVelocity() * BulletSpeed, GetActorLocation()); + Destroy(); +} \ No newline at end of file diff --git a/Source/M4_CPP/public/M4_PlayerPawn.h b/Source/M4_CPP/public/M4_PlayerPawn.h index a123353..4266e29 100644 --- a/Source/M4_CPP/public/M4_PlayerPawn.h +++ b/Source/M4_CPP/public/M4_PlayerPawn.h @@ -1,5 +1,6 @@ #pragma once +#include "M4_Projectile.h" #include "CoreMinimal.h" #include "GameFramework/Pawn.h" #include "InputAction.h" @@ -11,6 +12,7 @@ UCLASS() class AM4_PlayerPawn : public APawn { GENERATED_BODY() + public: AM4_PlayerPawn(); @@ -31,6 +33,9 @@ protected: UPROPERTY() UInputAction* ShootAction; + UPROPERTY(VisibleAnywhere) + AM4_Projectile* Proj; + //bool HasShot = false; float MoveSpeed = 500.f; FVector2D MeshScale = FVector2D(0.6f, 0.5f); diff --git a/Source/M4_CPP/public/M4_Projectile.h b/Source/M4_CPP/public/M4_Projectile.h index 912c97b..4bd7e08 100644 --- a/Source/M4_CPP/public/M4_Projectile.h +++ b/Source/M4_CPP/public/M4_Projectile.h @@ -22,13 +22,22 @@ class M4_CPP_API AM4_Projectile : public AActor public: AM4_Projectile(); - -protected: - float bullSpeed = 50.f; - float lifeTime = 3.f; - -public: - + + // Called every frame + //virtual void Tick(float DeltaTime) override; + + // Despawn after 5 seconds by default + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Projectile | Lifespan") + float ProjectileLifespan = 5.0f; + + // The amount of force this projectile imparts on objects it collides with + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Projectile | Physics") + float BulletSpeed = 100.0f; + + // Mesh of the projectile in the world + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Projectile | Mesh") + TObjectPtr ProjectileMesh; + UFUNCTION() void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,FVector Impulse, const FHitResult& Hit); UMeshComponent* GetCollisionComp() const { return MeshComp; }