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->SetRelativeScale3D(FVector(1.0f, MeshScale.Y, MeshScale.X));
static ConstructorHelpers::FObjectFinder<UStaticMesh> DefaultMeshRef(TEXT("/Game/CTP/04_Mesh/SM_Cube.SM_Cube"));
if (DefaultMeshRef.Succeeded())
{
@@ -85,22 +84,26 @@ void AM4_PlayerPawn::Move(const FInputActionInstance& Instance)
void AM4_PlayerPawn::Shoot(const FInputActionInstance& Inst)
{
if (Inst.GetValue().Get<bool>() == true)
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Using the Wand!"));
UWorld* const World = GetWorld();
if (!Proj && !World)
if (Proj)
{
FVector TargetPosition = GetActorLocation();
FRotator SpawnRotation = GetActorRotation();
UWorld* World = GetWorld();
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Blue, TEXT("Using the Wand!"));
FActorSpawnParameters ActorSpawnParams;
ActorSpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding;
ActorSpawnParams.Owner = this;
if (World)
{
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 "Components/MeshComponent.h"
// Sets default values
AM4_Projectile::AM4_Projectile()
{
ProjectileMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComp"));
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);
PrimaryActorTick.bCanEverTick = true;
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)
{
OtherComp->AddImpulseAtLocation(GetVelocity() * BulletSpeed, GetActorLocation());
Destroy();
//OtherComp->AddImpulseAtLocation(GetVelocity() * BulletSpeed, GetActorLocation());
}
// 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 Shoot(const FInputActionInstance& Inst);
UPROPERTY(VisibleAnywhere)
TSubclassOf<AM4_Projectile> Proj = AM4_Projectile::StaticClass();
protected:
UPROPERTY(VisibleAnywhere)
@@ -33,9 +36,6 @@ protected:
UPROPERTY()
UInputAction* ShootAction;
UPROPERTY(VisibleAnywhere)
TSubclassOf<AM4_Projectile> Proj;
float MoveSpeed = 500.f;
FVector2D MeshScale = FVector2D(0.6f, 0.5f);
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
@@ -6,40 +6,33 @@
#include "GameFramework/Actor.h"
#include "M4_Projectile.generated.h"
class UProjectileMovementComponent;
class UMeshComponent;
UCLASS()
class M4_CPP_API AM4_Projectile : public AActor
{
GENERATED_BODY()
UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
UMeshComponent* MeshComp;
UPROPERTY(VisibleAnywhere,BlueprintReadOnly, Category = Movement, meta = (AllowPrivateAccess = "true"))
UProjectileMovementComponent* ProjectileMovem;
UPROPERTY(VisibleAnywhere, Category = Movement)
UProjectileMovementComponent* ProjectileMovementComponent;
public:
AM4_Projectile();
// Called every frame
//virtual void Tick(float DeltaTime) override;
virtual void Tick( float DeltaTime ) override;
// Despawn after 5 seconds by default
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Projectile | Lifespan")
float ProjectileLifespan = 3.0f;
UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
UMeshComponent* CollisionComp;
// The amount of force this projectile imparts on objects it collides with
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Projectile | Physics")
float BulletSpeed = 100.0f;
UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
UStaticMeshComponent* ProjectileMeshComponent;
// Mesh of the projectile in the world
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Projectile | Mesh")
TObjectPtr<UStaticMeshComponent> ProjectileMesh;
UPROPERTY(VisibleDefaultsOnly, Category = Movement)
UMaterialInstanceDynamic* ProjectileMat;
UFUNCTION()
void ShootDirection(const FVector& ShootDir);
void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,FVector Impulse, const FHitResult& Hit);
UMeshComponent* GetCollisionComp() const { return MeshComp; }
UProjectileMovementComponent* GetMovementComp() const { return ProjectileMovem; }
};