How could I make simple VR movement?

I’m currently working on a VR game like Hands. I have made practically everything accept movement. I don’t know how to go about this so any help/information will be useful.

1 Like

Do you want to allow only left to right and front to back?

I have a script that has front to back support that I’ll add here;
Only problem is it only supports front to back, meaning you can’t move left or right or up and down, as my script was made for normal sized humanoids in mind

local VRService = game:GetService('VRService')
local UserInputService = game:GetService('UserInputService')

local Camera = workspace.CurrentCamera
Camera.CameraType = Enum.CameraType.Scriptable
Camera.CFrame = CFrame.new(0,10,0)

local function gprtc(cf)
	return Camera.CFrame*CFrame.new(cf.p*Camera.HeadScale) * (cf - cf.p)
end

game:GetService("RunService").Stepped:Connect(function()
	-- Move the player, this should be done first so there isn't a delay when moving
	local MoveSpeed
	if UserInputService:IsGamepadButtonDown(Gamepad, Enum.KeyCode.ButtonL3) then
		MoveSpeed = 1.5
	else
		MoveSpeed = 1
	end
	
	local offsetCFrame = CFrame.new(gprtc(VRService:GetUserCFrame(Enum.UserCFrame.Head)).LookVector)
	Camera.CFrame = (Camera.CFrame:ToWorldSpace(CFrame.new(offsetCFrame.X/3*MoveY*MoveSpeed, 0, offsetCFrame.Z/3*MoveY*MoveSpeed)))
end)

UserInputService.InputChanged:Connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.Thumbstick1 then
			MoveY = input.Position.Y
		end
	end
end)

This should be put into a LocalScript.

Hope it helps!

4 Likes

On line 15 you use Gamepad, this is erroring with nil so what am I supposed to set that to. Also thanks.

2 Likes

Sorry for late response, add

local Gamepad = Enum.UserInputType.Gamepad1

to the top of the code.

4 Likes

hello again, how do I make it go up and down? It seems to only go forwards and backwards.

2 Likes

ive edited that script a bit and now it gos up and down. ive never worked with vr before but this worked for me (im using rift s)


wait(4)-- For loading.
local VRService = game:GetService('VRService')
local UserInputService = game:GetService('UserInputService')
local Gamepad = Enum.UserInputType.Gamepad1
local Camera = workspace.CurrentCamera
Camera.CameraType = Enum.CameraType.Scriptable
Camera.CFrame = CFrame.new(0,10,0)

local function gprtc(cf)
	return Camera.CFrame*CFrame.new(cf.p*Camera.HeadScale) * (cf - cf.p)
end

game:GetService("RunService").Stepped:Connect(function()
	-- Move the player, this should be done first so there isn't a delay when moving
	local MoveSpeed
	local MoveSpeedr -- Secondary movement
	if UserInputService:IsGamepadButtonDown(Gamepad, Enum.KeyCode.ButtonL3) then
		MoveSpeed = 1.5
	else
		MoveSpeed = 1
	end
	if UserInputService:IsGamepadButtonDown(Gamepad, Enum.KeyCode.ButtonR3) then
		MoveSpeedr = 1.5
	else
		MoveSpeedr = 1
	end
	local offsetCFrame = CFrame.new(gprtc(VRService:GetUserCFrame(Enum.UserCFrame.Head)).LookVector)
	local offsetCFrameR = CFrame.new(gprtc(VRService:GetUserCFrame(Enum.UserCFrame.Head)).RightVector)
	Camera.CFrame = (Camera.CFrame:ToWorldSpace(CFrame.new(offsetCFrame.X/3*MoveY*MoveSpeed, 0, offsetCFrame.Z/3*MoveY*MoveSpeed)))
	Camera.CFrame = (Camera.CFrame:ToWorldSpace(CFrame.new(0,  offsetCFrameR.Z/3*MoveYR*MoveSpeedr,0)))
	Camera.CFrame = (Camera.CFrame:ToWorldSpace(CFrame.new(offsetCFrame.X/3*MoveX*MoveSpeed, 0, offsetCFrame.Z/3*MoveX*MoveSpeed)))
end)

UserInputService.InputChanged:Connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.Thumbstick1 then
			MoveY = input.Position.Y
			MoveX = input.Position.X
		end
		if input.KeyCode == Enum.KeyCode.Thumbstick2 then
			MoveYR = input.Position.Y

		end
	end
end)
3 Likes

I have found a way myself, but yours is probably better lol thanks.

i think they mean Enym.UserInputType.Gamepad1. Im not 100 percent sure, though