UE5中物体(instancemesh)沿样条线(spline)循环移动

 UE5中物体(instancemesh)沿样条线(spline)循环移动_第1张图片

// 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);
	}
}

 

你可能感兴趣的:(ue5,java,服务器)