文章来源:https://www.toymoban.com/news/detail-806774.html
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/SplineComponent.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MeshMovingOnSplineClass.generated.h"
/**
*
*/
UCLASS()
class TCPCONNENT_API UMeshMovingOnSplineClass : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable)
static void MeshOnSplineMoving(int32 MeshCount,UInstancedStaticMeshComponent* InstancedStaticMesh,USplineComponent* spline,float Speed = 1);
static FTransform SpawnTransform;
static float InitSpeed;
static FVector SplineVector;
static FRotator SplineRotator;
static FVector SplineScale;
static FTransform UpdateTransform;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MeshMovingOnSplineClass.h"
#include "Components/InstancedStaticMeshComponent.h"
float UMeshMovingOnSplineClass::InitSpeed;
FTransform UMeshMovingOnSplineClass::SpawnTransform;
FVector UMeshMovingOnSplineClass::SplineVector;
FRotator UMeshMovingOnSplineClass::SplineRotator;
FVector UMeshMovingOnSplineClass::SplineScale = FVector(1,1,1);
FTransform UMeshMovingOnSplineClass::UpdateTransform;
void UMeshMovingOnSplineClass::MeshOnSplineMoving(int32 MeshCount, UInstancedStaticMeshComponent* InstancedStaticMesh,
USplineComponent* spline, float Speed)
{
if (InstancedStaticMesh->GetInstanceCount() < MeshCount)
{
for (int i = InstancedStaticMesh->GetInstanceCount(); i < MeshCount; i++)
{
InstancedStaticMesh->AddInstance(SpawnTransform,true);
}
}
InitSpeed = InitSpeed + Speed;
for (int j = 0; j < MeshCount; j++)
{
float AA = spline->GetSplineLength() / MeshCount;
float BB = j * AA;
float CC = BB + InitSpeed;
float DD = fmodf(CC,spline->GetSplineLength());
SplineVector = spline->GetWorldLocationAtDistanceAlongSpline(DD);
SplineRotator = spline->GetWorldRotationAtDistanceAlongSpline(DD);
UpdateTransform.SetLocation(SplineVector);
UpdateTransform.SetRotation(SplineRotator.Quaternion());
UpdateTransform.SetScale3D(SplineScale);
InstancedStaticMesh->UpdateInstanceTransform(j,UpdateTransform,true,true,false);
}
}
文章来源地址https://www.toymoban.com/news/detail-806774.html
到了这里,关于UE5中物体(instancemesh)沿样条线(spline)循环移动的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!