The way to Make a part follow the players VR controller is really easy and only requires less than 200 lines to make a part follow the players VR Controller heres how to do it
1 Create a new local script in Startercharacter scripts (“Which is in StarterPlayer”) and call it anything you like.
2 empty the script and do local UserInputService = game:GetService("UserInputService")
3 type these all in for the hands
local LHand = Instance.new("Part")
LHand.Size = Vector3.new(0.5,0.5,0.5)
LHand.Anchored = true
LHand.Parent = game.Players.LocalPlayer.Character
LHand.CanCollide = false
local RHand = Instance.new("Part")
RHand.Size = Vector3.new(0.5,0.5,0.5)
RHand.Anchored = true
RHand.Parent = game.Players.LocalPlayer.Character
RHand.CanCollide = false
local cam = game.Workspace.CurrentCamera
4 type this into it for it to track the players controller
UserInputService.UserCFrameChanged:Connect(function(hand,move)
if hand == Enum.UserCFrame.LeftHand then
LHand.CFrame = cam.CFrame * move
end
end)
UserInputService.UserCFrameChanged:Connect(function(hand,move)
if hand == Enum.UserCFrame.RightHand then
RHand.CFrame = cam.CFrame * move
end
end)
and you should have a working VR tracking Next i will do a tutorial on how to make a Grapple Gun with VR
8 Likes
Mind explaining what the code does?
This isn’t really much of a tutorial if we just get provided code that we don’t know how its supposed to work.
1 Like
just changes the cframe of the part to the UserCframe of the controller
1 Like
UserInputService.UserCFrameChanged:Connect(function(hand,move)
if hand == Enum.UserCFrame.LeftHand then
LHand.CFrame = cam.CFrame * move
end
if hand == Enum.UserCFrame.RightHand then
RHand.CFrame = cam.CFrame * move
end
end)
This is great! Do you know how to make joystick locomotion? If so I would love to know how!
2 Likes
This doesn’t really explain or show how people can implement VR mechanics into their game, this is a
piece of code that gives you block hands in VR. Not to say that this is bad, but it could use some explanation on what’s going on behind the scenes for those who are new.
For example : VRService is an api that devs can use to recalibrate or retrieve your hand’s / head’s position and rotation. Below this is test code that shows other variables that can affect how your VR game will run, as well has the basic functionality of VRService.
local VRService = game:GetService("VRService")
local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera
local HeadScale = 2
-- Headscale is like a size multiplier; imagine if HeadScale was 5, then you would be 5 times as big in game.
-- Setting the camera's type to scriptable means we can detach it from our character
Camera.CameraType = Enum.CameraType.Scriptable
Camera.CameraSubject = nil
Camera.HeadScale = HeadScale
--[[ What is the headset's position?
Your headset position is the relative position from your playspace to your actual headset
If you walk out 5 studs infront of your playspace, then inside the game you will be 5 studs infront of where-ever the camera is.
Camera and headset are seperate, remember that
]]
function Scale(CFrame_, Scale)
-- When scaling a cframe, we only need to scale it's position
return CFrame.new(CFrame_.Position*Scale) * CFrame_.Rotation
end
-- We are using RenderStepped to update our VR code before each frame is rendered
RunService.RenderStepped:Connect(function()
local HeadCFrame = VRService:GetUserCFrame(Enum.UserCFrame.Head)
local RightHandCFrame = VRService:GetUserCFrame(Enum.UserCFrame.RightHand)
local LeftHandCFrame = VRService:GetUserCFrame(Enum.UserCFrame.LeftHand)
-- When using GetUserCFrame, you will want to select the device you're trying to get the coordinants from
-- You can do this by adding your UserCFrame enum inside the angle brackets
-- Enum.UserCFrame.Head will refer to the player's headset
-- Enum.UserCFrame.RightHand will refer to the player's right controller
-- Enum.UserCFrame.LeftHand will refer to the player's left controller
-- Now we scale the user cframes to match our size
HeadCFrame = Scale(HeadCFrame, HeadScale)
RightHandCFrame = Scale(RightHandCFrame, HeadScale)
LeftHandCFrame = Scale(LeftHandCFrame, HeadScale)
Camera.CFrame = CFrame.new(0, 10, 0)
end)
4 Likes
Hello thank you for this! I figured out how it works and everything so its pretty great! Anyhow when ever I go in first person these parts disappear, I tried setting LocalTransparencyModifier to 0 but that didnt help. Do you know how I can fix this?