Minor - Apparition du projectile dans la scène à partir du joueur

Le joueur peut enfin faire apparaître le projectile dans la scène après avoir appuyé sur la touche "Espace".
Cependant le Mesh n'apparait toujours pas pour l'instant mais j'ai presque fini.
This commit is contained in:
NisemonoQ
2025-10-27 11:31:52 +01:00
parent c7702b9f64
commit 365727aa4c
7 changed files with 96 additions and 55 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -21,7 +21,6 @@ AM4_PlayerPawn::AM4_PlayerPawn()
MeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); MeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
MeshComponent->SetCollisionProfileName(UCollisionProfile::Pawn_ProfileName); MeshComponent->SetCollisionProfileName(UCollisionProfile::Pawn_ProfileName);
MeshComponent->SetRelativeScale3D(FVector(1.0f, MeshScale.Y, MeshScale.X)); MeshComponent->SetRelativeScale3D(FVector(1.0f, MeshScale.Y, MeshScale.X));
static ConstructorHelpers::FObjectFinder<UStaticMesh> DefaultMeshRef(TEXT("/Game/CTP/04_Mesh/SM_Cube.SM_Cube")); static ConstructorHelpers::FObjectFinder<UStaticMesh> DefaultMeshRef(TEXT("/Game/CTP/04_Mesh/SM_Cube.SM_Cube"));
if (DefaultMeshRef.Succeeded()) if (DefaultMeshRef.Succeeded())
@@ -85,22 +84,26 @@ void AM4_PlayerPawn::Move(const FInputActionInstance& Instance)
void AM4_PlayerPawn::Shoot(const FInputActionInstance& Inst) void AM4_PlayerPawn::Shoot(const FInputActionInstance& Inst)
{ {
if (Inst.GetValue().Get<bool>() == true) if (Proj)
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Using the Wand!"));
UWorld* const World = GetWorld();
if (!Proj && !World)
{ {
FVector TargetPosition = GetActorLocation(); UWorld* World = GetWorld();
FRotator SpawnRotation = GetActorRotation(); GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Blue, TEXT("Using the Wand!"));
FActorSpawnParameters ActorSpawnParams; if (World)
ActorSpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding; {
ActorSpawnParams.Owner = this; FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
const FVector InitialLocation = FVector(0.0f, GetActorLocation().Y, GetActorLocation().Z);
GetWorld()->SpawnActor<AM4_Projectile>(Proj, TargetPosition, SpawnRotation, ActorSpawnParams); AM4_Projectile* Projectile = World->SpawnActor<AM4_Projectile>(Proj, InitialLocation, FRotator(), SpawnParams);
FVector LaunchDirection = FVector(0.0f, InitialLocation.Y + 1.f,InitialLocation.Z);
if (Projectile)
{
Projectile->ShootDirection(LaunchDirection);
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, TEXT("Spell Casted!"));
}
}
} }
};
}
};

View File

@@ -1,26 +1,71 @@
// Fill out your copyright notice in the Description page of Project Settings. #include "M4_Projectile.h"
#include "M4_Projectile.h"
#include "GameFramework/ProjectileMovementComponent.h" #include "GameFramework/ProjectileMovementComponent.h"
#include "Components/MeshComponent.h" #include "Components/MeshComponent.h"
// Sets default values // Sets default values
AM4_Projectile::AM4_Projectile() AM4_Projectile::AM4_Projectile()
{ {
ProjectileMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComp")); PrimaryActorTick.bCanEverTick = true;
ProjectileMesh-> SetRelativeScale3D(FVector(1.f, .1f,.1f));
ProjectileMesh->BodyInstance.SetCollisionProfileName(TEXT("Projectile"));
ProjectileMesh->OnComponentHit.AddDynamic(this, &AM4_Projectile::OnHit);
ProjectileMesh->SetupAttachment(RootComponent);
ProjectileMesh->SetMobility(EComponentMobility::Movable);
ProjectileMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
if(!RootComponent)
{
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("ProjectileSceneComponent"));
}
if(!CollisionComp)
{
CollisionComp = CreateDefaultSubobject<UMeshComponent>(TEXT("MeshComponent"));
RootComponent = CollisionComp;
}
if(!ProjectileMovementComponent)
{
ProjectileMovementComponent = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovementComponent"));
ProjectileMovementComponent->SetUpdatedComponent(CollisionComp);
ProjectileMovementComponent->InitialSpeed = 700.0f;
ProjectileMovementComponent->MaxSpeed = 700.0f;
ProjectileMovementComponent->bRotationFollowsVelocity = true;
ProjectileMovementComponent->bShouldBounce = false;
ProjectileMovementComponent->ProjectileGravityScale = 0.0f;
}
if (!ProjectileMovementComponent)
{
ProjectileMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ProjectileMeshComponent"));
static ConstructorHelpers::FObjectFinder<UStaticMesh>Mesh(TEXT("'/Script/Engine.StaticMesh'/Game/CTP/04_Mesh/SM_Projectile.SM_Projectile'"));
if(Mesh.Succeeded())
{
ProjectileMeshComponent->SetStaticMesh(Mesh.Object);
}
static ConstructorHelpers::FObjectFinder<UMaterial>Material(TEXT("'/Script/Engine.Material'/Game/CTP/05_Material/M_Player.M_Player'"));
if (Material.Succeeded())
{
ProjectileMat = UMaterialInstanceDynamic::Create(Material.Object, ProjectileMeshComponent);
}
ProjectileMeshComponent->SetMaterial(0, ProjectileMat);
ProjectileMeshComponent->SetRelativeScale3D(FVector(.2f, .2f, .2f));
ProjectileMeshComponent->SetupAttachment(RootComponent);
}
}
void AM4_Projectile::ShootDirection(const FVector& ShootDir)
{
ProjectileMovementComponent->Velocity = ShootDir * ProjectileMovementComponent->InitialSpeed;
} }
void AM4_Projectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector Impulse, const FHitResult& Hit) void AM4_Projectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector Impulse, const FHitResult& Hit)
{ {
OtherComp->AddImpulseAtLocation(GetVelocity() * BulletSpeed, GetActorLocation()); //OtherComp->AddImpulseAtLocation(GetVelocity() * BulletSpeed, GetActorLocation());
Destroy(); }
// Called every frame
void AM4_Projectile::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
} }

View File

@@ -21,6 +21,9 @@ public:
virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override; virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override;
void Move(const FInputActionInstance& Instance); void Move(const FInputActionInstance& Instance);
void Shoot(const FInputActionInstance& Inst); void Shoot(const FInputActionInstance& Inst);
UPROPERTY(VisibleAnywhere)
TSubclassOf<AM4_Projectile> Proj = AM4_Projectile::StaticClass();
protected: protected:
@@ -32,9 +35,6 @@ protected:
UPROPERTY() UPROPERTY()
UInputAction* ShootAction; UInputAction* ShootAction;
UPROPERTY(VisibleAnywhere)
TSubclassOf<AM4_Projectile> Proj;
float MoveSpeed = 500.f; float MoveSpeed = 500.f;
FVector2D MeshScale = FVector2D(0.6f, 0.5f); FVector2D MeshScale = FVector2D(0.6f, 0.5f);

View File

@@ -1,4 +1,4 @@
// Fill out your copyright notice in the Description page of Project Settings. #include "GameFramework/ProjectileMovementComponent.h"
#pragma once #pragma once
@@ -6,40 +6,33 @@
#include "GameFramework/Actor.h" #include "GameFramework/Actor.h"
#include "M4_Projectile.generated.h" #include "M4_Projectile.generated.h"
class UProjectileMovementComponent;
class UMeshComponent; class UMeshComponent;
UCLASS() UCLASS()
class M4_CPP_API AM4_Projectile : public AActor class M4_CPP_API AM4_Projectile : public AActor
{ {
GENERATED_BODY() GENERATED_BODY()
UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
UMeshComponent* MeshComp;
UPROPERTY(VisibleAnywhere,BlueprintReadOnly, Category = Movement, meta = (AllowPrivateAccess = "true")) UPROPERTY(VisibleAnywhere, Category = Movement)
UProjectileMovementComponent* ProjectileMovem; UProjectileMovementComponent* ProjectileMovementComponent;
public: public:
AM4_Projectile(); AM4_Projectile();
virtual void Tick( float DeltaTime ) override;
UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
UMeshComponent* CollisionComp;
UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
UStaticMeshComponent* ProjectileMeshComponent;
// Called every frame UPROPERTY(VisibleDefaultsOnly, Category = Movement)
//virtual void Tick(float DeltaTime) override; UMaterialInstanceDynamic* ProjectileMat;
// Despawn after 5 seconds by default
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Projectile | Lifespan")
float ProjectileLifespan = 3.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 ShootDirection(const FVector& ShootDir);
void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,FVector Impulse, const FHitResult& Hit); void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,FVector Impulse, const FHitResult& Hit);
UMeshComponent* GetCollisionComp() const { return MeshComp; }
UProjectileMovementComponent* GetMovementComp() const { return ProjectileMovem; }
}; };