Minor - Bullet Spawning Method

La méthode est quasiment finie, il reste juste un pb au niveau de l'objet/la classe à appeler.
This commit is contained in:
NisemonoQ
2025-10-17 17:13:48 +02:00
parent f57f6b8832
commit 17c6475397
4 changed files with 44 additions and 25 deletions

View File

@@ -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<bool>() == 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<AM4_Projectile>(Proj, TargetPosition, SpawnRotation, ActorSpawnParams);
}
}
};

View File

@@ -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();
}

View File

@@ -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);

View File

@@ -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<UStaticMeshComponent> ProjectileMesh;
UFUNCTION()
void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,FVector Impulse, const FHitResult& Hit);
UMeshComponent* GetCollisionComp() const { return MeshComp; }