Hi, This is my first time trying to get vehicles to work with bodymovers.
Right now my helicopter can go up and down with Q and E, it can move forward with W and backwards with S and it can turn with A and D.
My problem now is that the helicopter isnt going forward relativly to its lookvector I believe.
This is my Explorer View
--Localscript
local ContextActionService = game:GetService("ContextActionService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local plane = script.Obj.Value
local gyro = plane.Main.BodyGyro
local vel = plane.Main.BodyVelocity
local ang = plane.Main.BodyAngularVelocity
local zeroVector3 = Vector3.new()
local RunService = game:GetService("RunService")
--up/down
local y = 0
--forward/backwards
local x = 0
--Turning
local z = 0
--I really dont know
local f = 0
--Variables
local speed = 50
--methodes
function MoveUp(action_name, action_state)
if action_state == Enum.UserInputState.Begin then
y = -10
end
if action_state == Enum.UserInputState.End then
y = 0
end
end
function MoveDown(action_name, action_state)
if action_state == Enum.UserInputState.Begin then
y = 10
end
if action_state == Enum.UserInputState.End then
y = 0
end
end
function MoveForward(action_name, action_state)
if action_state == Enum.UserInputState.Begin then
x = 15
f = -1
end
if action_state == Enum.UserInputState.End then
x = 0
f = 0
end
end
function MoveBackwards(action_name, action_state)
if action_state == Enum.UserInputState.Begin then
x = -15
f = 1
end
if action_state == Enum.UserInputState.End then
x = 0
f = 0
end
end
function MoveLeft(action_name, action_state)
if action_state == Enum.UserInputState.Begin then
z = 1
end
if action_state == Enum.UserInputState.End then
z = 0
end
end
function MoveRight(action_name, action_state)
if action_state == Enum.UserInputState.Begin then
z = -1
end
if action_state == Enum.UserInputState.End then
z = 0
end
end
--loop
RunService:BindToRenderStep("Update", Enum.RenderPriority.Input.Value, function()
local velocity = Vector3.new(x,y,0);
vel.Velocity = velocity
ang.AngularVelocity = Vector3.new(f,z,0);
end)
ContextActionService:BindAction("Up", MoveUp, false, "q")
ContextActionService:BindAction("Down", MoveDown, false, "e")
ContextActionService:BindAction("Forward", MoveForward, false, "w")
ContextActionService:BindAction("Backwards", MoveBackwards, false, "s")
ContextActionService:BindAction("Left", MoveLeft, false, "a")
ContextActionService:BindAction("Right", MoveRight, false, "d")
With a quick glance I think something along the line of vel.Velocity = lookVector * velocity would work. Where lookVector could be the LookVector of some main part in the Helicopter such as the Main part.
Sorry for the late response, I had some trouble getting your code to work with a simplified version of your plane. (and annoying issue of my internet persistently shutting.)
After trying like 10 things (and them not working) I decided that maybe we are going about this entirely wrong. Instead of using a BodyVelocity I suggest you use a BodyThrust
local velocity = Vector3.new(0, y, -x) * 50 --// 50 is just arbitrary debugging value that I added; you can remove it.
vel.Force = velocity
ang.AngularVelocity = Vector3.new(f, z, 0)
I appear to for some reason had to put the fowards / backwards thrust in the z coordinate and to have to flip the sign of the value.
(You may have to use Vector3.new(0, y, x) instead or Vector3.new(x, y, 0); I assume this issue is just because of how I built my plane.)
Here is a choppy clip of me driving my simplified plane with the code:
I am getting the LookVector of a part from my plane and adding the movement to it aswell as some speed variable.
What works? : it moves forward towards where it’s facing BUT it’s still not really working because it looks like it drives against an invisible wall. I can only go forward in 1 axis.
Likely because when the LookVector approaches 0, 0, 0 the result of the LookVector * otherVector will produce a Vector3 near 0, 0, 0. I don’t have any idea how to solve this problem but I have a solution that just ignores the problem and uses a different method.
You could default back to using a BodyThrust for moving forwards and backwards, and then use a BodyPosition or a BodyVelocity for moving up and down.
Alternatively instead of using a BodyVelocity or BodyThrust you can use a BodyPosition for thrusting where you would simply just set the Position property to LookVector * Force
How did you managed to get it work with BodyThrust?
I can’t get it to move forward, Turning it works fine but it’s not working moving forward even after trying some stuff out.
--Localscript
local ContextActionService = game:GetService("ContextActionService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local plane = script.Obj.Value
local RunService = game:GetService("RunService")
local height = 0
local turn = 0
local moving = 0
local vel = plane.Main.BodyThrust
local ang = plane.Main.BodyAngularVelocity
local speed = 20
function MoveUp(action_name, action_state)
if action_state == Enum.UserInputState.Begin then
height = -5
end
if action_state == Enum.UserInputState.End then
height = 0
end
end
function MoveDown(action_name, action_state)
if action_state == Enum.UserInputState.Begin then
height = 5
end
if action_state == Enum.UserInputState.End then
height = 0
end
end
function MoveLeft(action_name, action_state)
if action_state == Enum.UserInputState.Begin then
turn = 1
end
if action_state == Enum.UserInputState.End then
turn = 0
end
end
function MoveRight(action_name, action_state)
if action_state == Enum.UserInputState.Begin then
turn = -1
end
if action_state == Enum.UserInputState.End then
turn = 0
end
end
function MoveForward(action_name, action_state)
if action_state == Enum.UserInputState.Begin then
moving = -5
end
if action_state == Enum.UserInputState.End then
moving = 0
end
end
function MoveBackwards(action_name, action_state)
if action_state == Enum.UserInputState.Begin then
moving = 5
end
if action_state == Enum.UserInputState.End then
moving = 0
end
end
RunService:BindToRenderStep("Update", Enum.RenderPriority.Input.Value, function()
local velocity = Vector3.new(moving, 0, 0)
vel.Force = velocity
ang.AngularVelocity = Vector3.new(0, turn, 0)
end)
ContextActionService:BindAction("Up", MoveUp, false, "q")
ContextActionService:BindAction("Down", MoveDown, false, "e")
ContextActionService:BindAction("Left", MoveLeft, false, "a")
ContextActionService:BindAction("Right", MoveRight, false, "d")
ContextActionService:BindAction("Forward", MoveForward, false, "w")
ContextActionService:BindAction("Backwards", MoveBackwards, false, "s")
There are only 4 directions on a 2 dimensional plane and that is the type of terrain you are navigating when you want to go “forwards / backwards / left / right.”
I want the helicopter to always move forward even if im 45 degree turned. Right now it only moves forward when its 90 degree turned.
Edit: Its not like in your clip you’ve send.