Hey everyone. I am currently creating a build system that kinda combines Fortnight style building with a rust selection wheel. The problem currently is that my building placement feels very clunky. I think the problem is in the code where I generate the actors location:
FVector UCPPBuildComponent::CalcGridLocation()
{
int GridXY = 400;
int GridZ = 300;
float ForwardOffset = 1.5;
FVector Location = FVector(PCharacter->GetActorLocation().X * ForwardOffset, PCharacter->GetActorLocation().Y * ForwardOffset, 0);
int VectorX = ((FMath::RoundToInt(Location.X / GridXY)) * GridXY);
int VectorY = ((FMath::RoundToInt(Location.Y / GridXY)) * GridXY);
int VectorZ = ((FMath::RoundToInt(Location.Z / GridZ)) * GridZ);
return FVector(VectorX, VectorY, VectorZ) + FVector(0, 0, 20);
}
The above is called like this:
CurrentBuilding = GetWorld()->SpawnActor<ACPPBuilding>(BuildFloor, CalcGridLocation(), CalcGridRotation());
For reference this math is inspired by UnrealGaimeDev’s video (there are slight changes but perhaps I’m making a critical error somewhere?) The video starts at the logic’s location: [Eng] Let's Create Fortnite's Building System: Building Preview and Grid #08 - YouTube
This causes the actor to be spawned in the correct location but the distance between myself and the spawned actor increases the more the character moves to one direction. Here is a video showcasing it: Unreal Engine | 3D Math Problem with Build System - YouTube
Some more info:
The reason the actor moves further and further away is because of the ForwardOffset. If the player is at location x:20 and y:20 the actor is at x:30, y:30 so they have a difference of 10. Where if the player is at x:900 and y:900 the actor is at x:1350 and y:1350 (x * 1.5 = 900 * 1.5) so they have a difference of 450.
In the math I use round and that causes the whole thing to feel sluggish.
Thank you in advance. If there is any more details I can give please just ask and I’ll do my best to provide.