VR head oriented movement help

I have this movement script but I can’t figure out how to make the movement be dependant on the head’s orientation. I tried making it so the player moves in the direction they are facing but I’m not having any luck

UserInputService.InputChanged:Connect(function(input)

if input.UserInputType == Enum.UserInputType.Gamepad1 then

	if input.KeyCode == Enum.KeyCode.Thumbstick1 then

		thumbX = math.floor(input.Position.X  * 10) / 20
		thumbY = math.floor(input.Position.Y  * 10) / 20
		
		Camera.CFrame = Camera.CFrame * CFrame.new(thumbX,0,-thumbY)

	elseif input.KeyCode == Enum.KeyCode.Thumbstick2 then
		
		thumbY = math.floor(input.Position.Y  * 10) / 20
		
		Camera.CFrame = Camera.CFrame * CFrame.new(0,thumbY,0)

		if math.abs(input.Position.Y) > math.abs(input.Position.X) then
			thumbZ = math.floor(input.Position.Y * 10) / 20
		elseif not rotated and math.abs(input.Position.X) > 0.9 then
			rotated = true
			Camera.CFrame = Camera.CFrame * CFrame.Angles(0,-input.Position.X/3,0)
		end

	end


end
end)

Any help is appreciated

The code you have does not make any reference to the LookVector of the camera, aka in whch direction the camera is facing.
You’d want to multiply the Y input (forward/backward) by Camera.CFrame.LookVector and the X input (left/right) by Camera.CFrame.RightVector and then add those two together (and then take the Unit of that and multiply by the movement speed if you don’t want diagonal movement to be faster than movement in a single direction)

So all together it would look something like this:

local direction = (Camera.CFrame.LookVector * inputY + Camera.CFrame.RightVector * inputX).Unit
Camera.CFrame += direction * movementSpeed
1 Like

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