How do I get a movement vector based on the camera?

I need the a unit vector that points in the direction the camera is facing but is also based on the input.

In my code I have a table called input that has an x and a y value. These values can both be either -1, 0, or 1. So if the x input was -1 it would get left on that axis.

This is what the vector would look like without it being camera relative:

Vector3.new(input.X, 0, input.Y)

This is what it looks like with that, but I want that green vector line to be relative to the camera.

Sorry if this doesn’t make sense, I am having a hard time explaining it.

1 Like

This shouldn’t be too hard, I created a simple script which can make a specific part keep the same orientation as the camera.

For all axes

local Camera = workspace.CurrentCamera
local Part = workspace.Part
Camera:GetPropertyChangedSignal"CFrame":Connect(function()
	Part.CFrame = CFrame.new(Part.Position)*CFrame.fromOrientation(Camera.CFrame:ToOrientation())
end)

For only on the Y axis

local Camera = workspace.CurrentCamera
local Part = workspace.Part
Camera:GetPropertyChangedSignal"CFrame":Connect(function()
	local y = select(2,Camera.CFrame:ToOrientation())
	Part.CFrame = CFrame.new(Part.Position)*CFrame.fromOrientation(0,y,0)
end)

This shouldn’t be too hard to add,

All axes but accounting for the offset

local input = {X=0,Y=1,Z=-1}
local Camera = workspace.CurrentCamera
local Part = workspace.Part
Camera:GetPropertyChangedSignal"CFrame":Connect(function()
	Part.CFrame = CFrame.new(Part.Position)*CFrame.fromOrientation(Camera.CFrame:ToOrientation())*CFrame.Angles((input.X/2)*math.pi,(input.Y/2)*math.pi,(input.Z/2)*math.pi)
end)

Only the y axis but accounting for the offset

local input = {X=0,Y=1,Z=-1}
local Camera = workspace.CurrentCamera
local Part = workspace.Part
Camera:GetPropertyChangedSignal"CFrame":Connect(function()
	local y = select(2,Camera.CFrame:ToOrientation())
	Part.CFrame = CFrame.new(Part.Position)*CFrame.fromOrientation(0,y,0)*CFrame.Angles((input.X/2)*math.pi,(input.Y/2)*math.pi,(input.Z/2)*math.pi)
end)
2 Likes

I need a vector not an orientation.

You can just get the .LookVector of the CFrame.

Either way I am not going to create a part. I could just create a CFrame but is there really no better way?

No need to create a part

just a cframe variable, does the same job but without extra listeners and memory

local cf = CFrame.new(0,0,0)

-- use the cf as your reference

“I could just create a CFrame”

My bad, I overlooked that part.

You could try getting the angle difference between camera cframe and a dummy cframe of (0,0,0)