[NOT FIXED]Moving part relativly to lookvector

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 image

--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")

Here is my Problem in a video: https://i.gyazo.com/22cc27ad9b35a12004b2ad9af5edb88d.mp4

Also every little help or improvment is appreciated! I just started :))
For example tipps for the acceleration and so on.

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.

I tried that but that lead to some weird movement.
Currently I am taking the lookvector of the “main” part of the plane.

vel.Velocity = velocity * plane.Main.CFrame.LookVector

https://i.gyazo.com/2dc5853db5a6ded0cd3fe5314a301313.mp4
(its hard to see in the GIF but the helicopter isnt moving forward and only very limited, like a kind of invisible box when im 90 degree which wont let me trough)

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:

1 Like

Oh wow that looks alot better, May I get the place to see the code? :))

Thanks so much in advance!

If not then it is fine too, I’ll figure it out in some time I hope ^^

Oh, sorry I was pretty busy and just got back.

I posted the code in my second post.

1 Like

Alright there are a few problems with your methode with using BodyThrust instead of BodyVelocity.

But I am nearly done to make it 100% work with BodyVelocity .
Basicly this is my current lope code

RunService:BindToRenderStep("Update", Enum.RenderPriority.Input.Value, function()
	print(plane.Engine.CFrame.LookVector)
	vel.Velocity = plane.Engine.CFrame.LookVector * Vector3.new(-moving,height,0) * speed
	ang.AngularVelocity = Vector3.new(0,turn,0)
end)

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.

Problem:
https://i.gyazo.com/d3591cc3cbc25e6359a78f8c5410b5c5.mp4

Also how would I apply a height value to make it go up/down?

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")

Sorry if this is taking to long.

I already said this but I’ll iterate myself once more.

I posted the code that I changed that showed how I got it to work with BodyThrust

The code would go here

1 Like

I got that and it kinda works now, but its not moving in every direction. It can only move forward in 4 directions and I really don’t know why.

In this video I am always pressing forward aswell as turning.
https://i.gyazo.com/cad1fda5e18126f41742dea6c38ed15f.mp4

code:

local velocity = Vector3.new(0, 0, moving) * 100
	
	tru.Force = velocity
	ang.AngularVelocity = Vector3.new(0, turn, 0)
	vel.Velocity = Vector3.new(0,height,0)

I don’t understand what you mean.

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.

Could you atleast send me the placefile from your working plane so I can check it out and find my problem?

I did not save a copy of the place file, sorry. I simply just posted the code and closed studio after I finally got it to work.

I tried checking auto saves too just in case and there was nothing there. I do not see why your problem would occur either.

Me nether. Ahh it’s so frustrating being nearly there and only this problem occures . .