UE5中物体(instancemesh)沿样条线(spline)循环移动
// 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://blog.csdn.net/weixin_42318094/article/details/135632585
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!