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

@@ -22,7 +22,6 @@ AM4_PlayerPawn::AM4_PlayerPawn()
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,21 +84,25 @@ 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* World = GetWorld();
UWorld* const World = GetWorld(); GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Blue, TEXT("Using the Wand!"));
if (!Proj && !World)
if (World)
{ {
FVector TargetPosition = GetActorLocation(); FActorSpawnParameters SpawnParams;
FRotator SpawnRotation = GetActorRotation(); SpawnParams.Owner = this;
const FVector InitialLocation = FVector(0.0f, GetActorLocation().Y, GetActorLocation().Z);
FActorSpawnParameters ActorSpawnParams; AM4_Projectile* Projectile = World->SpawnActor<AM4_Projectile>(Proj, InitialLocation, FRotator(), SpawnParams);
ActorSpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding; FVector LaunchDirection = FVector(0.0f, InitialLocation.Y + 1.f,InitialLocation.Z);
ActorSpawnParams.Owner = this;
GetWorld()->SpawnActor<AM4_Projectile>(Proj, TargetPosition, SpawnRotation, ActorSpawnParams);
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")); if(!RootComponent)
ProjectileMesh->OnComponentHit.AddDynamic(this, &AM4_Projectile::OnHit); {
ProjectileMesh->SetupAttachment(RootComponent); RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("ProjectileSceneComponent"));
ProjectileMesh->SetMobility(EComponentMobility::Movable); }
ProjectileMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
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

@@ -22,6 +22,9 @@ public:
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:
UPROPERTY(VisibleAnywhere) UPROPERTY(VisibleAnywhere)
@@ -33,9 +36,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);
FVector2D LastMoveValue = FVector2D::ZeroVector; FVector2D LastMoveValue = FVector2D::ZeroVector;

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();
// Called every frame virtual void Tick( float DeltaTime ) override;
//virtual void Tick(float DeltaTime) override;
// Despawn after 5 seconds by default UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Projectile | Lifespan") UMeshComponent* CollisionComp;
float ProjectileLifespan = 3.0f;
// The amount of force this projectile imparts on objects it collides with UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Projectile | Physics") UStaticMeshComponent* ProjectileMeshComponent;
float BulletSpeed = 100.0f;
// Mesh of the projectile in the world UPROPERTY(VisibleDefaultsOnly, Category = Movement)
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Projectile | Mesh") UMaterialInstanceDynamic* ProjectileMat;
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; }
}; };