How would I go about creating VR turning based on the players thumbstick on the X Axis?

How would I go about creating VR turning based on the players thumbstick on the X Axis?

What am I trying to achieve?
Long title? I know. Since Roblox supported VR on standalone, I can now develop games with ease without hopping on my old rusty computer :sweat_smile:. Recently, I’ve been trying to implement VR turning and I have no idea how I would do it. I am trying to achieve snap or smooth-like turning based on which way the VR controller right thumbstick goes on the x axis (Left or Right) I’ve been having a lot of issues with this because mainly for one, I am new to VR scripting and for 2 How would I track the players thumbstick movement?

What I have done so far?
So far, my movement script, I’ve kept it simple:

local Character = script.Parent

local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Head = Character:WaitForChild("Head")

Humanoid.WalkSpeed = 0
Humanoid.JumpPower = 0

local UIS = game:GetService("UserInputService")
local VRService = game:GetService("VRService")
local RunService = game:GetService("RunService")
local HapticService = game:GetService("HapticService")

local HeadScale = 1

local RightHaptic = 0
local LeftHaptic = 0

local Height = 5

local Holding = {}

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

local Gamepad = Enum.UserInputType.Gamepad1

local Hands = game.ReplicatedStorage:FindFirstChild("Hands"):Clone()
Hands.Parent = Character

local AnimController = Hands:FindFirstChild("AnimationController")

local RightHand = Hands:FindFirstChild("RightHand")
local LeftHand = Hands:FindFirstChild("LeftHand")

local VR_WalkSpeed
local MoveY = 0

game.StarterGui:SetCore("VRLaserPointerMode", 0)
game.StarterGui:SetCore("VREnableControllerModels", false)

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

local function CalculateScale(CFrame_, Scale)
	return CFrame.new(CFrame_.Position * Scale) * CFrame_.Rotation
end

function Lerp(A, B, C)
	return A + (B - A) * C
end

RunService.RenderStepped:Connect(function(Delta)
	local HandCF_R = VRService:GetUserCFrame(Enum.UserCFrame.RightHand)
	local HandCF_L = VRService:GetUserCFrame(Enum.UserCFrame.LeftHand)
	
	if UIS:IsGamepadButtonDown(Gamepad, Enum.KeyCode.ButtonL3) then
		VR_WalkSpeed = 1.5
	else
		VR_WalkSpeed = 1
	end

	local offsetCFrame = CFrame.new(CalculateCF(VRService:GetUserCFrame(Enum.UserCFrame.Head)).LookVector)
	Camera.CFrame = (Camera.CFrame:ToWorldSpace(CFrame.new(offsetCFrame.X / 3 * MoveY * VR_WalkSpeed, 0, offsetCFrame.Z / 3 * MoveY * VR_WalkSpeed)))
	
	RightHand.CFrame = (Camera.CFrame * CFrame.new(HandCF_R.p * HeadScale)) * CFrame.fromEulerAnglesXYZ(HandCF_R:ToEulerAnglesXYZ())
	LeftHand.CFrame = (Camera.CFrame * CFrame.new(HandCF_L.p * HeadScale)) * CFrame.fromEulerAnglesXYZ(HandCF_L:ToEulerAnglesXYZ())
end)

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

This is my basic movement VR system, all it needs is turning, specifically in the Input section that is right here:

UIS.InputChanged:Connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.Thumbstick1 then
			MoveY = input.Position.Y
		else
			MoveY = 0
		end
       -- input implementation for turning
	end
end)

If anyone could help me answer this, I don’t need any code fixing just need some help. It would immensely help me out and other people trying to implement vr as well! :pray:

Do not use input.UserInputType, it only needs:

if input.KeyCode == Enum.KeyCode.Thumbstick1 then
if input.Delta > 0 then
-- turning code here (right i think)
elseif input.Delta < 0 then
-- turning code here (left i think)
end
end

p.s use this if you don’t want smooth rotation

Note that you can use input.Position too, but input.Delta is better to remember in terms of thumbsticks.

Thanks, I’ll try this method out. This is the current code I have. I’ll tell you if it works or not while I am debugging it.

UIS.InputChanged:Connect(function(input, processed)
	if input.KeyCode == Enum.KeyCode.Thumbstick2 then
		if input.Delta > 0 then
			Camera.CFrame = Camera.CFrame * CFrame.Angles(math.rad(0),90,0)
		elseif input.Delta < 0 then
			Camera.CFrame = Camera.CFrame * CFrame.Angles(math.rad(0),-90,0)
		end
	end
end)

I tried it and It didn’t work. Is there any errors to my script I showed above?

I forgot that Delta is a Vector3 ._.

UIS.InputChanged:Connect(function(input, processed)
	if input.KeyCode == Enum.KeyCode.Thumbstick2 then
		if input.Delta.X > 0 then
			Camera.CFrame = Camera.CFrame * CFrame.Angles(math.rad(0),90,0)
		elseif input.Delta.X < 0 then
			Camera.CFrame = Camera.CFrame * CFrame.Angles(math.rad(0),-90,0)
		end
	end
end)

The only thing I changed is that I added .X to input.Delta to check the rotation of the thumbstick on the X axis.

1 Like

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