Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with optimising a plane-script's turnspeed (?)

Asked by 5 years ago

Hey there Robloxians.

I am currently trying to script a flying-script for a plane. I am only using a small brick to simulate a plane for the time being. - I will just be calling it the "plane brick" from now on.

A bit of information about how the script works: I have a local script that gets the mouse-position (mouse.Hit.p) whenever the player holds "Button1Down" and "mouse.Move". The mouse-position is then sent to the mainscript via a RemoteEvent.

I also have 2 BodyMovers in the "plane brick": a BodyGyro (to rotate the plane) and a BodyVelocity (to generate the speed/ force).

Here is the mainscript:

local part = game.ReplicatedStorage.PlaneBrick:Clone()
part.Parent = game.Workspace
local partPos = part.Position

    while wait (0.1) do
script.RemoteEvent.OnServerEvent:Connect(function(player, mousehit) -- "mousehit" is the mouse-position I talked about.
    local gyro = part.BodyGyro
    gyro.MaxTorque = Vector3.new (math.huge, math.huge, math.huge)
    gyro.CFrame = CFrame.new(part.Position, mousehit)
    wait(0.5)
    part.BodyVelocity.Velocity = part.CFrame.LookVector * 12 -- and this is where my problem is: The script works, but it is not very optimal, since I had to base the BodyVelocity’s direction on the LookVector, I start getting problems when I try to increase the speed. Because whenever I click somewhere, the PlaneBrick will then start rotating, and then a few seconds afterward the PlaneBrick's direction will also change (so they don’t do it synchronously).

--I tried using other alternatives that didn't require the LookVector (So I don’t have to wait for the rotation to be completed, before it can change the direction correctly).
--Here is an example of one of the alternatives I tried, that didn't work: 
--part.BodyVelocity.Velocity = Vector3.new (mousehit.x, mousehit.y, mousehit.z ) * 12
--(The position/ direction and speed should both be alright, but for some reason, it doesn't work).
end)

Any help will be very much appreciated :-)

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Nice job! This is a very clearly worded question with more than enough information to answer :)

First and foremost, get rid of that while loop and replace it with one run by the game's runservice - it's considered much better practice, and has no chance of hanging unlike a while loop with no wait() ;).

--Replace
while wait() do
...
end
--With this:
function loop()
...
end
game:GetService("RunService").Heartbeat:Connect(loop) --This will make the loop run once per frame - usually 30-60 times per second

Secondly, you really shouldn't be running your :Connect function inside your loop - you only need to run it once to connect your function to a remote event.

As far as smoothly changing the bodyvelocity to make your block act like a plane, you definitely had the right idea - you just put it in the wrong place. Because the bodyvelocity is dependent on the angle the part is at, it should be constantly updated with the part's orientation - not just once like you have it right now. The best (only?) way to do this is to have it constantly update inside your loop

Here's your code with all the changes implemented

local part = game.ReplicatedStorage.PlaneBrick:Clone()
part.Parent = game.Workspace
local partPos = part.Position

function update() --Replace the while loop with a function - this is linked to the game's runservice. 
    part.BodyVelocity.Velocity = part.CFrame.LookVector * 12
    --Rest of the stuff inside the while loop goes here.
end
game:GetService("RunService").Heartbeat:Connect(update)

--You don't need to run this Connect every loop iteration!
script.RemoteEvent.OnServerEvent:Connect(function(player, mousehit) -- "mousehit" is the mouse-position I talked about.
    local gyro = part.BodyGyro
    gyro.MaxTorque = Vector3.new (math.huge, math.huge, math.huge)
    gyro.CFrame = CFrame.new(part.Position, mousehit)
end)

Edit:

Good questions! First of all, the reason we don't want to set bodyVelocity using "part.BodyVelocity.Velocity = Vector3.new (mousehit.x, mousehit.y, mousehit.z ) * 12" is because mousehit isn't a direction at all - it's a point. Think about this: If mouseHit is at (0,0,0), and we're 10 blocks off in the X direction at (10,0,0), what would happen if we used that formula? It would become "part.BodyVelocity.Velocity = Vector3.new (0, 0, 0 ) * 12" - or just 0, making us stop completely when we actually want to move.

Secondly, the reason the reason the bodyvelocity's direction isn't set instantly is because it is dependent on the bodygyro. All the loop is responsible for is constantly updating the bodyvelocity to align with the direction of the part's lookvector (ie, the part is always moving in the direction it's facing). Because the part's lookvector - and by extension the direction is facing - is controlled by the bodyGyro, the bodyVelocity is, in a roundabout way, controlled by the bodyGyro - making sure they never desync! So, as bodyGyro rotates the part to face towards the new target, the loop will update the bodyVelocity to drive the part in the direction it's facing that frame (bodyGyro will take more than a few to get the part on target)- creating a nice curve just like you'd expect from a plane.

0
Heartbeat doesn't run at thirty frames mate, this depends on the server's performance, which is mostly 50-60fps. Mirzadaswag 110 — 5y
0
Ah, you're right - stepped/heartbeat are tied to framerate. Answer has been edited to reflect that. Thanks! whenallthepigsfly 541 — 5y
0
Thanks a lot! You are a lifesaver, and a real MVP ;-) henrikknudsen 2 — 5y
0
I have a few questions tho: First off, why wasn't it possible to change the BodyVelocity.Velocity, using: “part.BodyVelocity.Velocity = Vector3.new (mousehit.x, mousehit.y, mousehit.z ) * 12” –- as we both had a direction and a speed? Secondly, if the loop now runs 30 to 60 times per second/ each frame. How come that the LookVector doesn’t instantly set an too-early direction - henrikknudsen 2 — 5y
View all comments (3 more)
0
- ,before the BodyGyro can rotate probably? henrikknudsen 2 — 5y
0
I've updated my answer whenallthepigsfly 541 — 5y
0
Thanks again! I first saw your answer/edit now, but it still means a lot to me, that you took even more time to further explain it to me! :-) henrikknudsen 2 — 5y
Ad

Answer this question