bonjour,
Dans un projet Unreal Engine v5.4 First Person C++ nommé toto,je crée une nouvelle classe de nom MovableActor:
MovableActor.h:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/StaticMeshComponent.h"
#include "MovableActor.generated.h"
UCLASS()
class TOTO_API AMovableActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMovableActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Variable pour la vitesse de translation
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
float Speed = 100.0f;
// Vecteur direction de la translation, paramétrable dans Details
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
FVector TranslationDirection = FVector(0.0f, 0.0f, 1.0f); // Valeur par défaut (vers le haut)
// Propriété pour spécifier l'objet à translater
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Translation")
AActor* TargetActorToTranslate = nullptr; // Utilisez le type spécifique si vous le connaissez (ex: AStaticMeshActor*)
private:
// Fonction pour effectuer la translation
void PerformTranslation(float DeltaTime); // Déclaration de la fonction
// Fonction pour effectuer la translation (ancienne version)
void TranslateTaggedActors(float DeltaTime);
};
MovableActor.cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "MovableActor.h"
#include "Kismet/GameplayStatics.h"
// Sets default values
AMovableActor::AMovableActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
// Vous pouvez ajouter un StaticMeshComponent ici si vous voulez que cet acteur ait une représentation visuelle
// StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
// SetRootComponent(StaticMeshComponent);
}
// Called when the game starts or when spawned
void AMovableActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMovableActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
PerformTranslation(DeltaTime);
}
void AMovableActor::PerformTranslation(float DeltaTime)
{
// Translation de l'acteur cible spécifié
if (TargetActorToTranslate != nullptr && TargetActorToTranslate != this)
{
FVector Translation = TranslationDirection * Speed * DeltaTime;
TargetActorToTranslate->AddActorWorldOffset(Translation, true);
}
else
{
// Translation des acteurs avec le tag "translat" (si aucun acteur cible n'est spécifié)
TArray<AActor*> FoundActors;
UGameplayStatics::GetAllActorsWithTag(GetWorld(), FName("translat"), FoundActors);
FVector Translation = TranslationDirection * Speed * DeltaTime;
for (AActor* TaggedActor : FoundActors)
{
if (TaggedActor && TaggedActor != this) // S'assurer que l'acteur n'est pas nul et n'est pas cet acteur lui-même
{
TaggedActor->AddActorWorldOffset(Translation, true); // Le 'true' ici active le balayage de collision
}
}
}
}
La compilation (pas celle en direct) fonctionne.
Je glisse MovableActor dans la scène puis
remplis dans la colonne droite la direction de translation,le champ speed et choisis l’acteur sur lequel s’effectue la translation
L’acteur choisi ne se déplace pas !
D’où vient le problème?
merci de votre aide