-
What do you want to achieve? Keep it simple and clear!
Vr Body to collide with walls that have CanCollide turned On. -
What is the issue? Include screenshots / videos if possible!
I keep phasing through everything -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Devfourms and vr body templates.
VRClientHandler Code:
-- Services --
local ContextActionService = game:GetService("ContextActionService")
local VRService = game:GetService("VRService")
local UserInputService = game:GetService("UserInputService")
local StarterGui = game:GetService("StarterGui")
local HapticService = game:GetService("HapticService")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local VRLib = require(ReplicatedStorage.VRLib)
local plr = game.Players.LocalPlayer
-- some variables
local scale = 1
local SpeedDivider = 10
local height = 4
local Turn = CFrame.fromEulerAnglesXYZ(0, 0, 0)
local ThumbStick1 = Vector3.new(0, 0, 0)
-- Check if player has VR
if VRService.VREnabled then
warn("["..plr.Name.."] VR detected")
ReplicatedStorage.Remote.VR_PlayerJoined:FireServer(scale, true)
else
warn("["..plr.Name.."] no VR detected")
return
end
-- Remove Roblox VR Gui
pcall(function()
StarterGui:SetCore("VRLaserPointerMode", 0)
StarterGui:SetCore("VREnableControllerModels", false)
end)
-- Setup camera
local Camera = workspace.CurrentCamera
Camera.CameraType = Enum.CameraType.Scriptable
Camera.CameraSubject = nil
Camera.CFrame = CFrame.new(0, height, 0)
Camera.HeadScale = scale
-- get char head and set transparency
local char = workspace:WaitForChild(plr.Name)
if char then
char:WaitForChild("Head", 30).Transparency = 1
end
-- Main Loop
RunService.RenderStepped:Connect(function(deltaTime)
local HeadCFrame, RightCFrame, LeftCFrame = VRLib.GetUserCFrames()
local Head, Right, Left = VRLib.CalculateUserCFrames(HeadCFrame, RightCFrame, LeftCFrame, Camera.CFrame)
ReplicatedStorage.Remote.VR_UpdatePosition:FireServer(Head, Right, Left)
VRLib.ApplyUserCFrames(char.Head,char.LeftHand,char.RightHand,Head,Right,Left)
VRLib.MoveCharacter(Camera, char, "Head", ThumbStick1, SpeedDivider)
Camera.CFrame = CFrame.new(Camera.CFrame.X, height, Camera.CFrame.Z) * (Camera.CFrame.Rotation)
Turn = CFrame.fromEulerAnglesXYZ(0, 0, 0)
ReplicatedStorage.Remote.VR_Touching:FireServer(char)
end)
ContextActionService:BindAction("Thumbstick1",function(Name, State, Input)
ThumbStick1 = Vector3.new(Input.Position.X, 0, -Input.Position.Y)
end, false, Enum.KeyCode.Thumbstick1)
turndeb = false
-- This action for some reason switches direction at random times and I cannot for the life of me figure out why
ContextActionService:BindAction("Thumbstick2",function(Name, State, Input)
if turndeb == false then
if Input.Position.Y > 0 then
turndeb = true
Camera.CFrame = Camera.CFrame * CFrame.fromEulerAnglesXYZ(0, math.rad(45), 0)
--print(Camera.CFrame)
repeat wait() until Input.Position.Y == 0
turndeb = false
elseif Input.Position.Y < 0 then
turndeb = true
Camera.CFrame = Camera.CFrame * CFrame.fromEulerAnglesXYZ(0, math.rad(-45), 0)
--print(Camera.CFrame)
repeat wait() until Input.Position.Y == 0
turndeb = false
end
end
end, false, Enum.KeyCode.Thumbstick2)
ContextActionService:BindAction("LeftGrip", function(Name, State, Input)
if State == Enum.UserInputState.Begin then
ReplicatedStorage.Remote.VR_Grabbed:FireServer("LeftHand", "begin")
elseif State == Enum.UserInputState.End then
ReplicatedStorage.Remote.VR_Grabbed:FireServer("LeftHand", "end")
end
end, false, Enum.KeyCode.ButtonL1)
ContextActionService:BindAction("RightGrip", function(Name, State, Input)
if State == Enum.UserInputState.Begin then
ReplicatedStorage.Remote.VR_Grabbed:FireServer("RightHand", "begin")
elseif State == Enum.UserInputState.End then
ReplicatedStorage.Remote.VR_Grabbed:FireServer("RightHand", "end")
end
end, false, Enum.KeyCode.ButtonR1)
ContextActionService:BindAction("OpenMenu", function(Name, State, Input)
VRLib.CreatePointer(game.Players.LocalPlayer)
end, false, Enum.KeyCode.ButtonX)
VRServerHandler Code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PhysicsService = game:GetService("PhysicsService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local VRLib = require(ReplicatedStorage.VRLib)
function NetworkTouch(Player, Character, Hit)
pcall(function()
if Hit.CanCollide then
if Hit.Anchored == true then
Hit.Anchored = false
Hit:SetNetworkOwner(Player)
Hit.Anchored = true
else
Hit:SetNetworkOwner(Player)
end
end
end)
end
ReplicatedStorage.Remote.VR_PlayerJoined.OnServerEvent:Connect(function(plr, Scale, getAvatarAppearance)
VRLib.CreateCharacter(plr, Scale, getAvatarAppearance)
local char = workspace:FindFirstChild(plr.Name)
char.LeftHand.Touched:Connect(function(hit)
NetworkTouch(plr, char, hit)
end)
char.LeftHand.Grip.Touched:Connect(function(hit)
NetworkTouch(plr, char, hit)
end)
char.RightHand.Touched:Connect(function(hit)
NetworkTouch(plr, char, hit)
end)
char.RightHand.Grip.Touched:Connect(function(hit)
NetworkTouch(plr, char, hit)
end)
end)
ReplicatedStorage.Remote.VR_UpdatePosition.OnServerEvent:Connect(function(plr, HeadCFrame, RightCFrame, LeftCFrame)
VRLib.ApplyUserCFrames(workspace:FindFirstChild(plr.Name).Head,workspace:FindFirstChild(plr.Name).LeftHand,workspace:FindFirstChild(plr.Name).RightHand,HeadCFrame,RightCFrame,LeftCFrame)
end)
ReplicatedStorage.Remote.VR_Grabbed.OnServerEvent:Connect(function(plr, hand, state)
local char = workspace:FindFirstChild(plr.Name)
local HandObj = char:FindFirstChild(hand)
if state == "begin" then
for i, v in pairs(HandObj.Grip:GetTouchingParts()) do
if v:FindFirstChild("Grabbable") then
NetworkTouch(plr, char, v)
local weld = Instance.new("WeldConstraint")
weld.Parent = HandObj.Grip
weld.Part0 = HandObj.Grip
weld.Part1 = v
weld.Name = "Weld"
if v:FindFirstChild("ResetOrientationOnGrab") then
v.Position = HandObj.Position
v.Orientation = HandObj.Orientation
end
break
end
end
elseif state == "end" then
if HandObj.Grip:FindFirstChild("Weld") then
local obj = HandObj.Grip.Weld.Part1
local lastpos = obj.Position
HandObj.Grip.Weld:Destroy()
end
end
end)
ReplicatedStorage.Remote.VR_Touching.OnServerEvent:Connect(function(plr, char)
for i, v in pairs(char.LeftHand:GetTouchingParts()) do
NetworkTouch(plr, char, v)
end
for i, v in pairs(char.RightHand:GetTouchingParts()) do
NetworkTouch(plr, char, v)
end
end)