Why are the vectors here not working?

I am making a submarine test for my game, and when you hold W it is supposed to move you forwards, I also have D and A as keys to rotate the submarine

However, as you see in the video, for some reason it is moving in the wrong direction (the correct direction is supposed to be the LookVector of the model)

Relevant Code (handles keybind processing):

local keybinds = {
	W = function(down: boolean)
		local movementMultiplier = 10
		local damping = 0.2
		
		local modelPivot = model:GetPivot()
		local lookVector = modelPivot.LookVector.Unit
		local rightVector = modelPivot.RightVector.Unit
		
		if down then
			cancelFuncs.W = moveAlongVector(lookVector*movementMultiplier, model, damping)
		else
			if not cancelFuncs.W then return end
			isFinishing = true
			local finished = cancelFuncs.W()
			finished:Once(function()
				cancelFuncs.W = nil
				isFinishing = false
			end)
		end
	end,
	A = function(down: boolean)
		local degreeIncrement = 10
		local damping = 0.2

		local modelPivot = model:GetPivot()
		local lookVector = modelPivot.LookVector.Unit
		local rightVector = modelPivot.RightVector.Unit
		local upVector = modelPivot.UpVector.Unit

		if down then
			cancelFuncs.A = rotateAboutVector(Vector3.yAxis, model, degreeIncrement, damping)
		else
			if not cancelFuncs.A then return end
			isFinishing = true
			local finished = cancelFuncs.A()
			finished:Once(function()
				cancelFuncs.A = nil
				isFinishing = false
			end)
		end
	end,
	S = function(down: boolean)
		local movementMultiplier = 2
		local damping = 0.2

		local modelPivot = model:GetPivot()
		local lookVector = modelPivot.LookVector.Unit
		local rightVector = modelPivot.RightVector.Unit

		if down then
			cancelFuncs.S = moveAlongVector(-lookVector*movementMultiplier, model, damping)
		else
			if not cancelFuncs.S then return end
			isFinishing = true
			local finished = cancelFuncs.S()
			finished:Once(function()
				cancelFuncs.S = nil
				isFinishing = false
			end)
		end
	end,
	D = function(down: boolean)
		local degreeIncrement = -10
		local damping = 0.2

		local modelPivot = model:GetPivot()
		local lookVector = modelPivot.LookVector.Unit
		local rightVector = modelPivot.RightVector.Unit
		local upVector = modelPivot.UpVector.Unit

		if down then
			cancelFuncs.D = rotateAboutVector(Vector3.yAxis, model, degreeIncrement, damping)
		else
			if not cancelFuncs.D then return end
			isFinishing = true
			local finished = cancelFuncs.D()
			finished:Once(function()
				cancelFuncs.D = nil
				isFinishing = false
			end)
		end
	end,
}

Feel free to ask for clarification, i am just very stumped on how to fix this

Can i see moveAlongVector function?

function TransformModule.MoveAlongVector(Vector: Vector3, Model: Model, EndDamping: number) -- Uses alignpositions to continuously move along a vector. Returns a cancel function
	local Speed = Vector.Magnitude
	
	local Loaded = false
	local DoMove = true
	
	local FinishedSignal = Instance.new("BindableEvent")
	
	local MoverThread = nil
	MoverThread = task.spawn(function()
		while true do
			local ModelPivot = Model:GetPivot()
			local TargetCFrame = ModelPivot * CFrame.new(Vector)
			
			local Distance = (TargetCFrame.Position - ModelPivot.Position).Magnitude
			
			Loaded = true
			if DoMove then
				TransformModule.AlignPosition(TargetCFrame, Model, Distance/Speed, 100)
			else
				TransformModule.AlignPosition(ModelPivot * CFrame.new(Vector*1.5), Model, Distance/Speed, EndDamping)
				FinishedSignal:Fire()
				return
			end
		end
	end)
	repeat task.wait() until Loaded
	
	return function()
		DoMove = false
		return FinishedSignal.Event
	end
end

I don’t think it’s related to any of the functions i’m using, since they just take a vector and doesnt actually process what it is

Nevermind, I fixed it after learning about CFrame:VectorToObjectSpace()

function TransformModule.MoveAlongVector(Vector: Vector3, Model: Model, EndDamping: number) -- Uses alignpositions to continuously move along a vector. Returns a cancel function
	local Speed = Vector.Magnitude
	
	local Loaded = false
	local DoMove = true
	
	local FinishedSignal = Instance.new("BindableEvent")
	
	local MoverThread = nil
	MoverThread = task.spawn(function()
		while true do
			local ModelPivot = Model:GetPivot()
			local TargetVector = ModelPivot:VectorToObjectSpace(Vector)
			local TargetCFrame = ModelPivot * CFrame.new(TargetVector)
			
			local Distance = (TargetCFrame.Position - ModelPivot.Position).Magnitude
			
			Loaded = true
			if DoMove then
				TransformModule.AlignPosition(TargetCFrame, Model, Distance/Speed, 100)
			else
				TransformModule.AlignPosition(ModelPivot * CFrame.new(TargetVector*1.5), Model, Distance/Speed, EndDamping)
				FinishedSignal:Fire()
				return
			end
		end
	end)
	repeat task.wait() until Loaded
	
	return function()
		DoMove = false
		return FinishedSignal.Event
	end
end

I dont know how or why it works, but it works, so im marking this as a solution.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.