You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I’m trying to create a space ship system in the style of Starlink: Battle for Atlas. (I can’t find a short video showcasing the flight, but you can check it out for yourself on youtube to get the general idea). The important take away is that the space ship, while being on a planet, hovers above the ground and tries to maintain a certain altitude. -
What is the issue? Include screenshots / videos if possible!
Well, I can’t manage to make it work and I’ve been stuck for 3-4 months now: I tried the code on a dummy prototype, a simple part, and it works, yet when I try it out with the space ship, the same code (I tried playing around with the multipliers but it didn’t help) proves… bumpy to say the least.
you can see here how the space ship doesn’t behave the intended way, while the part (after I reload the game) keeps the same altitude (the red lines are showing the raycastings)
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried looking for solutions on Developer Hub or a long time, but I couldn’t find another similar issue to mine. Then I tried out this code, but it also didn’t work (+ I wasn’t such a huge fan of the way how the math operations were done.)
Finally I stumbled upon this and it’s how I wish my space ship would end up looking like, and that’s the method that works on the part in the video, but doesn’t for the space ship (no need to watch the entire video: just watch the segments with drawings starting at 18:20 and the one starting around 28:45 to understand the math that’s being used)
The code I’m using for both ship and dummy part (ofc I’m leaving out anything not related to issue and only focusing on the hovering part)
run.Stepped:Connect(function()
for _,thruster:BasePart in options.thrusters do
--The sphere's just a ball visualizing the raycasting
local sphere = spFolders:FindFirstChild(thruster.Name.."_Sphere") :: Part
local thrCf = thruster.CFrame
local thrPos = thrCf.Position
local targetPos = thrCf*direction + seat.AssemblyLinearVelocity*.1
local result = workspace:Raycast(thrPos, targetPos-thrPos, params)
local hoverForce = thruster:FindFirstChildWhichIsA("VectorForce", true) :: VectorForce
if result then
print("Ray "..thruster.Name.." found "..result.Instance.Name)
local reqForce = (result.Position-targetPos).Magnitude
sphere.Position = result.Position
sphere.BrickColor = BrickColor.Green()
hoverForce.Force = Vector3.yAxis*defaultForce*reqForce --Vector.yAxis is basically a vertical vector. and it's the formula of force = mass * acceleration, where the acceleration is gravity, and with addition of an offset, because else the part is just gravityless
else
sphere.Position = targetPos
sphere.BrickColor = BrickColor.Red()
hoverForce.Force = Vector3.yAxis*defaultForce/8
end
end
end)
This is what the 2 different scripts have in common. The spheres (for the ship) / lines (for the part) you saw on the videos are only there to visualize the raycastings and have no impact whatsoever for the actual problem.
If anyone has any idea/solution I’d love to hear it out, wish ya a nice day