So, I have made a grabbing system for VR and the throwing is broken for no reason.
The ball just falls when I try to throw it. This is not the same problem as VR picking up object is weird because the hands are physically simulated.
The grabbing system works by using a
WeldConstraint
on the object to hold. Then, it destroys the weld when you want to stop holding it. The hands move with AlignPosition
and AlignOrientation
. The player has network ownership of the ball.
Code:
local Players = game:GetService('Players')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local UserInputService = game:GetService('UserInputService')
local RunService = game:GetService('RunService')
local Anaglyph = ReplicatedStorage.Anaglyph
local AnaglyphPhyiscs = require(Anaglyph.Physics.AnaglyphPhysics)
local AnaglyphPositionalInput = require(Anaglyph.Input.AnaglyphPositionalInput)
local Player = Players.LocalPlayer
local HeadScale = workspace.CurrentCamera.HeadScale
local Height = 5 * HeadScale
local WalkSpeed = 14 * HeadScale
local RoomWorldCenter = CFrame.new(Vector3.new(workspace.SpawnLocation.Position.X, Height, workspace.SpawnLocation.Position.Z))
Player.CharacterAdded:Connect(function(Character)
local Humanoid = Character:WaitForChild('Humanoid')
local HumanoidRootPart = Character:WaitForChild('HumanoidRootPart')
local Head = Character:WaitForChild('Head')
local LeftHand = Character:WaitForChild('LeftHand')
local RightHand = Character:WaitForChild('RightHand')
LeftHand.GrabSphere.Velocity = Vector3.new(5, 5, 5)
RightHand.GrabSphere.Velocity = Vector3.new(5, 5, 5)
Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
local HumanoidOffest = (Head.Position - HumanoidRootPart.Position).Magnitude
local CenterRootAttachment = Instance.new('Attachment')
CenterRootAttachment.Name = 'CenterRootAttachment'
CenterRootAttachment.Position = Vector3.new(0, 0, 0)
CenterRootAttachment.Parent = HumanoidRootPart
local NoPhysicsRootPart = AnaglyphPhyiscs.AlignWithProxyPart(Character, Vector3.new(0, 0, 0), CenterRootAttachment)
local NoPhysicsLeftHand = AnaglyphPhyiscs.AlignWithProxyPart(Character, Vector3.new(0, 0, 0), LeftHand.GrabSphere.Attachment, 200, 200, 200)
local NoPhysicsRightHand = AnaglyphPhyiscs.AlignWithProxyPart(Character, Vector3.new(0, 0, 0), RightHand.GrabSphere.Attachment, 200, 200, 200)
RunService:BindToRenderStep('Tracking', Enum.RenderPriority.Input.Value, function()
local HeadsetWorldCFrame = AnaglyphPositionalInput.TrackVRCFrameRelativeToWorldCFrame(Enum.UserCFrame.Head, RoomWorldCenter, HeadScale)
NoPhysicsRootPart.CFrame = HeadsetWorldCFrame:ToWorldSpace(CFrame.new(Vector3.new(0, -HumanoidOffest, 0)))
local LeftHandWorldCFrame = AnaglyphPositionalInput.TrackVRCFrameRelativeToWorldCFrame(Enum.UserCFrame.LeftHand, RoomWorldCenter, HeadScale)
NoPhysicsLeftHand.CFrame = LeftHandWorldCFrame
local RightHandWorldCFrame = AnaglyphPositionalInput.TrackVRCFrameRelativeToWorldCFrame(Enum.UserCFrame.RightHand, RoomWorldCenter, HeadScale)
NoPhysicsRightHand.CFrame = RightHandWorldCFrame
end)
LeftHand.GrabSphere.Touched:Connect(function()
end)
RightHand.GrabSphere.Touched:Connect(function()
end)
UserInputService.InputChanged:Connect(function(Input, Processed)
if Input.KeyCode == Enum.KeyCode.Thumbstick1 then
local MovementDirection = (((Input.Position.X * Head.CFrame.RightVector) + (Input.Position.Y * Head.CFrame.LookVector)) * Vector3.new(1, 0, 1))
local WalkVector = (MovementDirection * WalkSpeed) / 90
RoomWorldCenter = RoomWorldCenter:ToWorldSpace(CFrame.new(WalkVector))
end
end)
UserInputService.InputBegan:Connect(function(Input, Processed)
if Input.KeyCode == Enum.KeyCode.ButtonL1 then
local MininumDistance
local ClosestPart
local Weld
for i, Part in pairs(LeftHand.GrabSphere:GetTouchingParts()) do
if Part:FindFirstAncestor(Player.Character.Name, true) == nil then
local Distance = (Part.Position - LeftHand.GrabSphere.Position).Magnitude
if MininumDistance == nil then
MininumDistance = Distance
ClosestPart = Part
elseif Distance < MininumDistance then
MininumDistance = Distance
ClosestPart = Part
end
end
end
if ClosestPart then
Weld = Instance.new('WeldConstraint')
Weld.Part0 = LeftHand.GrabSphere
Weld.Part1 = ClosestPart
Weld.Parent = LeftHand.GrabSphere
end
UserInputService.InputEnded:Connect(function(Input, Processed)
if Input.KeyCode == Enum.KeyCode.ButtonL1 then
if Weld then
Weld:Destroy()
end
end
end)
elseif Input.KeyCode == Enum.KeyCode.ButtonR1 then
local MininumDistance
local ClosestPart
local Weld
for i, Part in pairs(RightHand.GrabSphere:GetTouchingParts()) do
if Part:FindFirstAncestor(Player.Character.Name, true) == nil then
local Distance = (Part.Position - RightHand.GrabSphere.Position).Magnitude
if MininumDistance == nil then
MininumDistance = Distance
ClosestPart = Part
elseif Distance < MininumDistance then
MininumDistance = Distance
ClosestPart = Part
end
end
end
if ClosestPart then
Weld = Instance.new('WeldConstraint')
Weld.Part0 = RightHand.GrabSphere
Weld.Part1 = ClosestPart
Weld.Parent = RightHand.GrabSphere
end
UserInputService.InputEnded:Connect(function(Input, Processed)
if Input.KeyCode == Enum.KeyCode.ButtonR1 then
if Weld then
Weld:Destroy()
end
end
end)
end
end)
end)