You can use hover vehicle tutorial that you’ve linked as a base and modify hovering function. Something like this should work:
- Do a LineTrace from your magneto “wheel” from it’s root downwards, length of the LineTrace should be something reasonable where you think your magnetic force should be active
- Define a variable to represent a “sweetspot” height at which vehicle should hover, let’s call it DesiredHeight
- LineTrace give you a distance at which ray collided with something, this would be ActualHeight variable
- What we want to do now is construct a sort of spring which will push vehicle away if it’s too close to track and pull it back if it’s too far away from track
- let’s calculate spring ratio:
SpringRatio = (DesiredHeight - ActualHeight) / DesiredHeight
notice that spring ratio can have both negative and positive sign, this will pull or push spring - Spring itself can be described like this:
SpringForce = ActorUpVector * SpringStiffness * SpringRatio
we want to push vehicle upwards if SpringRatio is positive. SpringStiffness is what you need to tweak, good value to start from will be more or less equal to force of gravity acting on vehicle. SpringStiffness ~ MassOfVehicle * 980 / NumberOfMagnetoWheels, just calculate it in spreadsheet and later tweak it to be a bit higher/lower - At this point you could already apply SpringForce as AddForceAtLocation() to the root body, unfortunately it won’t work nicely because springs will keep oscillating, we need to add dampener and for this we need to change SpringForce formula:
SpringForce = ActorUpVector * SpringStiffness * SpringRatio - SpringDampening * SpringVelocity
SpringDampening is usually very small value in comparison to SpringStiffness, so if your SpringStiffness is somewhere around hundreds of thousands then SpringDampening would be few thousands.
But first we need to find SpringVelocity, for this, on every update you need to store ActualHeight, at the end of update, into a variable. Let’s call it PreviousActualHeight, so at the end of each update you just do PreviousActualHeight = ActualHeight.
Now we can find velocity:
SpringVelocity = (PreviousActualHeight - ActualHeight) / deltaTime - Finally calculate SpringForce and apply it as AddForceAtLocation(SpringForce, LocationOfMagnetoWheel) to the root body