How To Move Helicopter?
Asked by
5 years ago Edited 5 years ago
Hello fellow devs. I've run into something. I wrote this script which handles moving of part perfectly like , but I don't take turning into account.
05 | local UserInputService = game:GetService( "UserInputService" ) |
06 | local Players = game:GetService( "Players" ) |
09 | local Heli = script.Parent.Parent |
10 | local GyroScope = script.Parent |
11 | local Thrust = Heli:FindFirstChild( "BodyThrust" ) |
12 | local Reaction = Heli:FindFirstChild( "BodyReaction" ) |
13 | local Force = Heli:FindFirstChild( "BodyForce" ) |
16 | local m = Heli:GetMass() |
17 | local g = workspace.Gravity |
18 | local cf = CFrame.new() |
19 | local vector = Vector 3. new() |
22 | local enum = Enum.KeyCode |
25 | local Movement = { [ tostring (enum.T) ] = CFrame.Angles( 0 , 0 , math.rad( 20 )), |
26 | [ tostring (enum.F) ] = CFrame.Angles(math.rad( 10 ), 0 , 0 ), |
27 | [ tostring (enum.H) ] = CFrame.Angles(math.rad(- 10 ), 0 , 0 ), |
28 | [ tostring (enum.G) ] = CFrame.Angles( 0 , 0 , math.rad(- 20 )), |
29 | [ tostring (enum.Y) ] = Vector 3. new( 0 , 100 , 0 ), |
30 | [ tostring (enum.R) ] = Vector 3. new( 0 , - 100 , 0 ) |
33 | local Keys = { [ tostring (enum.T) ] = cf, |
34 | [ tostring (enum.F) ] = cf, |
35 | [ tostring (enum.G) ] = cf, |
36 | [ tostring (enum.H) ] = cf, |
37 | [ tostring (enum.Y) ] = vector, |
38 | [ tostring (enum.R) ] = vector |
42 | game:GetService( "RunService" ).Stepped:Connect( function () |
44 | local W = Keys [ tostring (enum.T) ] |
45 | local A = Keys [ tostring (enum.F) ] |
46 | local D = Keys [ tostring (enum.G) ] |
47 | local S = Keys [ tostring (enum.H) ] |
48 | local Y = Keys [ tostring (enum.Y) ] |
49 | local R = Keys [ tostring (enum.R) ] |
52 | Thrust.Force = Vector 3. new( 0 , 2 *m*g, 0 ) |
53 | Reaction.Force = -Vector 3. new( 0 , m*g, 0 ) |
55 | Heli.Velocity = Heli.Velocity - Heli.Velocity/ 10 |
59 | GyroScope.CFrame = W * A * S * D |
62 | UserInputService.InputBegan:Connect( function (input) |
64 | Keys [ tostring (input.KeyCode) ] = Movement [ tostring (input.KeyCode) ] |
68 | UserInputService.InputEnded:Connect( function (input) |
70 | Keys [ tostring (input.KeyCode) ] = cf |
https://gyazo.com/3b81764428846bf1bf6ebf1d26342a7b
So I added few things to this code to make it turn using BodyAngularVelocity.
01 | local AngularVelocity = Heli:FindFirstChild( "BodyAngularVelocity" ) |
04 | local m = Heli:GetMass() |
05 | local g = workspace.Gravity |
06 | local cf = CFrame.new() |
07 | local vector = Vector 3. new() |
10 | local enum = Enum.KeyCode |
14 | [ tostring (enum.T) ] = { CFrame.Angles( 0 , 0 , math.rad(- 20 )), Vector 3. new( 0 ,(m*g)/ 27 , 0 ) } , |
15 | [ tostring (enum.R) ] = { CFrame.Angles(math.rad( 10 ), 0 , 0 ), Vector 3. new( 0 ,(m*g)/ 67 , 0 ) } , |
16 | [ tostring (enum.Y) ] = { CFrame.Angles(math.rad(- 10 ), 0 , 0 ), Vector 3. new( 0 ,(m*g)/ 67 , 0 ) } , |
17 | [ tostring (enum.G) ] = { CFrame.Angles( 0 , 0 , math.rad( 20 )), Vector 3. new( 0 ,(m*g)/ 17 , 0 ) } , |
19 | [ tostring (enum.H) ] = { Vector 3. new( 0 , - 2 , 0 ), 0 } , |
20 | [ tostring (enum.F) ] = { Vector 3. new( 0 , 2 , 0 ), 0 } , |
22 | [ tostring (enum.J) ] = { Vector 3. new( 0 , 100 , 0 ), 0 } , |
23 | [ tostring (enum.LeftShift) ] = { Vector 3. new( 0 , - 100 , 0 ), 0 } |
27 | [ tostring (enum.T) ] = { cf, vector } , |
28 | [ tostring (enum.R) ] = { cf, vector } , |
29 | [ tostring (enum.Y) ] = { cf, vector } , |
30 | [ tostring (enum.G) ] = { cf, vector } , |
32 | [ tostring (enum.H) ] = { vector, vector } , |
33 | [ tostring (enum.F) ] = { vector, vector } , |
35 | [ tostring (enum.J) ] = { vector, vector } , |
36 | [ tostring (enum.LeftShift) ] = { vector, vector } |
41 | [ tostring (enum.T) ] = { cf, vector } , |
42 | [ tostring (enum.R) ] = { cf, vector } , |
43 | [ tostring (enum.Y) ] = { cf, vector } , |
44 | [ tostring (enum.G) ] = { cf, vector } , |
46 | [ tostring (enum.F) ] = { vector, vector } , |
47 | [ tostring (enum.H) ] = { vector, vector } , |
49 | [ tostring (enum.J) ] = { vector, vector } , |
50 | [ tostring (enum.LeftShift) ] = { vector, vector } |
57 | game:GetService( "RunService" ).Stepped:Connect( function () |
58 | local Position = Heli.Position |
59 | local dir = Heli.CFrame.LookVector |
61 | local W = Keys [ tostring (enum.T) ] |
62 | local A = Keys [ tostring (enum.F) ] |
63 | local S = Keys [ tostring (enum.G) ] |
64 | local D = Keys [ tostring (enum.H) ] |
65 | local Y = Keys [ tostring (enum.Y) ] |
66 | local R = Keys [ tostring (enum.R) ] |
67 | local J = Keys [ tostring (enum.J) ] |
68 | local Shift = Keys [ tostring (enum.LeftShift) ] |
71 | Thrust.Force = Vector 3. new( 0 , 2 *m*g, 0 ) |
72 | Reaction.Force = -Vector 3. new( 0 , m*g, 0 ) |
75 | Heli.Velocity = Heli.Velocity - Heli.Velocity/ 10 |
78 | AngularVelocity.AngularVelocity = A [ 1 ] + D [ 1 ] |
79 | Force.Force = J [ 1 ] + Shift [ 1 ] + W [ 2 ] + S [ 2 ] + R [ 2 ] + Y [ 2 ] |
81 | GyroScope.CFrame = W [ 1 ] * S [ 1 ] * R [ 1 ] * Y [ 1 ] |
84 | UserInputService.InputBegan:Connect( function (input) |
86 | Keys [ tostring (input.KeyCode) ] = Movement [ tostring (input.KeyCode) ] |
90 | UserInputService.InputEnded:Connect( function (input) |
92 | Keys [ tostring (input.KeyCode) ] = Nulls [ tostring (input.KeyCode) ] |
https://gyazo.com/af5d7a647b6b5968c30082a264119ba2
Here I added another Table to hold nulls, that I call values which should be applied when helicopter is stopped(or no key is pressed, to be precise). The problem I have to solve, is that I should rotate Gyro based on lookVector and constantly update it, but I have trouble figuring out how to do it with this code. I came up with this solution
05 | return CFrame.Angles(x, 0 , z) |
07 | return CFrame.Angles( 0 , 0 , 0 ) |
this is the function I made which would replace cframe.Angles in tables.
02 | [ tostring (enum.T) ] = { Cframe, Vector 3. new( 0 ,(m*g)/ 27 , 0 ) } , |
03 | [ tostring (enum.R) ] = { Cframe, Vector 3. new( 0 ,(m*g)/ 67 , 0 ) } , |
04 | [ tostring (enum.Y) ] = { Cframe, Vector 3. new( 0 ,(m*g)/ 67 , 0 ) } , |
05 | [ tostring (enum.G) ] = { Cframe, Vector 3. new( 0 ,(m*g)/ 17 , 0 ) } , |
07 | [ tostring (enum.H) ] = { Vector 3. new( 0 , - 2 , 0 ), 0 } , |
08 | [ tostring (enum.F) ] = { Vector 3. new( 0 , 2 , 0 ), 0 } , |
10 | [ tostring (enum.J) ] = { Vector 3. new( 0 , 100 , 0 ), 0 } , |
11 | [ tostring (enum.LeftShift) ] = { Vector 3. new( 0 , - 100 , 0 ), 0 } |
15 | [ tostring (enum.T) ] = { cf, vector } , |
16 | [ tostring (enum.R) ] = { cf, vector } , |
17 | [ tostring (enum.Y) ] = { cf, vector } , |
18 | [ tostring (enum.G) ] = { cf, vector } , |
20 | [ tostring (enum.H) ] = { vector, vector } , |
21 | [ tostring (enum.F) ] = { vector, vector } , |
23 | [ tostring (enum.J) ] = { vector, vector } , |
24 | [ tostring (enum.LeftShift) ] = { vector, vector } |
29 | [ tostring (enum.T) ] = { cf, vector } , |
30 | [ tostring (enum.R) ] = { cf, vector } , |
31 | [ tostring (enum.Y) ] = { cf, vector } , |
32 | [ tostring (enum.G) ] = { cf, vector } , |
34 | [ tostring (enum.F) ] = { vector, vector } , |
35 | [ tostring (enum.H) ] = { vector, vector } , |
37 | [ tostring (enum.J) ] = { vector, vector } , |
38 | [ tostring (enum.LeftShift) ] = { vector, vector } |
42 | local dir = heli.CFrame.LookVector |
44 | local W = Keys [ tostring (enum.T) ] |
45 | local S = Keys [ tostring (enum.G) ] |
46 | local Y = Keys [ tostring (enum.Y) ] |
47 | local R = Keys [ tostring (enum.R) ] |
49 | GyroScope.CFrame = W [ 1 ] (-(dir.X), -(dir.Z)) * S [ 1 ] (dir.X, dir.Z) * R [ 1 ] * Y [ 1 ] |
52 | UserInputService.InputBegan:Connect( function (input) |
54 | Keys [ tostring (input.KeyCode) ] = Movement [ tostring (input.KeyCode) ] |
58 | UserInputService.InputEnded:Connect( function (input) |
60 | Keys [ tostring (input.KeyCode) ] = Nulls [ tostring (input.KeyCode) ] |
The problem with this is, that if I release W key or S Key, in Nulls Table first element is just CFrame.new() so passing arguments like that is not possible.
Maybe there's some methods I don't know(which there clearly are), or I'm missing something obvious. Note:I use T, R, Y, G, H, F and J keys in tables, because they are not available in studio Run test mode.