2

I want to spawn actors on the mouse location, but I need them to be spawned on rounded to 0.5 x and y coordinates.

I tried multiplying by 2, rounding and dividing by 2, but it doesn't seem to work.

This is a screenshot of my BP. BluePrint

I expect my actors to be spawned only on rounded to 0.5 coordinates, but instead they spawn on any coordinates.

ynotxx
  • 25
  • 6

1 Answers1

1

To snap a vector to a uniform grid you should use the following formula:

GridPoint = Floor(Point / GridSize) * GridSze;

Where GridSize is a scalar and Point is your input. GridPoint is your output.

It's pretty easy to do with blueprints and the built in math library functions.

Basically just divide your position vector with the prefered grid size (Float) and round/floor it. Then multiply with the grid size and you have your position snapped to the nearest grid point.

UE4 Blueprint example:

enter image description here

UE4 C++

    // Define Grid Size
    float GridSize = 100.0f;

    // Get Actor Position
    FVector Position = GetActorLocation();

    // Calculate Grid Position
    FVector TmpPosition = FVector(Position / GridSize);
    FVector GridPoint = FVector(FMath::RoundHalfToZero(TmpPosition.X),FMath::RoundHalfToZero(TmpPosition.Y), FMath::RoundHalfToZero(TmpPosition.Z)) * GridSize;
    SetActorLocation(GridPoint);
Playdome.io
  • 3,137
  • 2
  • 17
  • 34