Download

[4.25.2] Using Splines in C++? (Resolved)

I have been trying to use splines in C++ to spawn a set of actors randomly along the curve of a spline.
So far all I have achieved is making a straight line with all of the actors being spawned at the endpoint.


I want the top spline, but can only seem to achieve the bottom spline.



{
    RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("SceneComp"));
    SpawningSpline = CreateDefaultSubobject<USplineComponent>(TEXT("Path"));
    SpawningSpline->SetupAttachment(RootComponent);
}



for (size_t i = 0; i < ActorCount; i++)
{
    SpawningSpline->ClearSplinePoints();
    FSplinePoint StartPoint;
    FSplinePoint EndPoint;

    StartPoint.InputKey = 0.0;
    StartPoint.Position = StartLoc;
    StartPoint.ArriveTangent = FVector(100.0F, 0.0F, 0.0F);
    StartPoint.LeaveTangent = FVector(100.0F, 0.0F, 0.0F);
    StartPoint.Rotation = StartDir.Rotation();
    StartPoint.Type = ESplinePointType::Curve;

    EndPoint.InputKey = 1.0;
    EndPoint.Position = StartPoint.Position + (EndDir * Distance);
    EndPoint.ArriveTangent = FVector(100.0F, 0.0F, 0.0F);
    EndPoint.LeaveTangent = FVector(100.0F, 0.0F, 0.0F);
    EndPoint.Rotation =FRotator(0.0F, 135.0F, 0.0F);
    EndPoint.Type = ESplinePointType::Curve;

    SpawningSpline->AddPoint(StartPoint, true);
    SpawningSpline->AddPoint(EndPoint, true);

    SpawnLocation = SpawningSpline->GetWorldLocationAtDistanceAlongSpline(FMath::FRandRange(0.0F, SpawningSpline->Duration));
}

What do I need to do to achieve the following:

  1. Give the Spline a curve
  2. Get a SpawnLocation that isn’t the Start or End Point, but rather a random location along the curve between the Start and End Point.

Any help with this would very much be appreciated.

A spline with two points is a line.

You can make it a curve by adjusting the tangents of each point, or you can add a (third) midpoint with a position at the top of the curve.

I was able to get a curve by increasing the strength of the tangents.
(apparently the tangents I was using were too small and it was just that their adjustments to the spline was not noticeable)

But I still have not found a way to spawn my set of actors randomly along the curve. They all spawn at the start or end point.

EDIT:
Nevermind, I got them to spawn along the line. I had assumed GetWorldLocationAtDistanceAlongSpline() wanted a percent of the splines length. When I input a raw distance it worked fine.