So I’m working on my VR gun game, and I’m redoing the slides/bolts because they only worked correctly facing forward. And at the time of posting, this is my issue:
For some reason it only works correctly facing forward in the world and when I move my hand forward or backwards in world space. Here is my code:
(Ignore anything with spline, that’s working just fine.)
(basically ignore anything after the line that I require the magSpline)
origin is the slide origin on the gun
I KNOW FOR A FACT THAT IT IS SOMETHING TO DO WITH THE ORIGIN AND NEWHAND CFRAME!
if RightBolt then
local slider = RightBolt.Parent:FindFirstChild("BoltSlider")
local origin = RightBolt:FindFirstChild("BoltOrigin").CFrame
local originP = origin.Position
local min = slider:FindFirstChild("Min").Value
local max = slider:FindFirstChild("Max").Value
local hand = UserCFrame.RightHand
local handPos = hand.Position
local newHand = CFrame.new(origin.X,origin.Y,hand.Z)
local newHandPos = newHand.Position
local zDifference = origin.Z-newHand.Z
print(zDifference)
local zPer = math.clamp(math.abs(zDifference/origin.Z)*SlideMultiplier,0,1)
print(zPer)
local magSpline = require(RightBolt.Parent:FindFirstChild("SplinePoints"):FindFirstChild("Bolt"):FindFirstChild("Spline"))
local newCF = magSpline:getPoint(zPer)
if zDifference < 0 then
slider.C1 = newCF
end
end
if RightBolt then
local slider = RightBolt.Parent:FindFirstChild("BoltSlider")
local origin = RightBolt:FindFirstChild("BoltOrigin").CFrame
local originP = origin.Position
local min = slider:FindFirstChild("Min").Value
local max = slider:FindFirstChild("Max").Value
local hand = UserCFrame.RightHand
local handPos = hand.Position
local newHand = CFrame.new(origin.X,origin.Y,hand.Z)
local newHandPos = newHand.Position
local xDifference = origin.X-newHand.X
print(xDifference)
local xPer = math.clamp(math.abs(xDifference/origin.X)*SlideMultiplier,0,1)
print(xPer)
local magSpline = require(RightBolt.Parent:FindFirstChild("SplinePoints"):FindFirstChild("Bolt"):FindFirstChild("Spline"))
local newCF = magSpline:getPoint(zPer)
if zDifference < 0 then
slider.C1 = newCF
end
end
local main = script.Parent.main
local part = script.Parent.Part
local function IsBehind(Part1,Part2)
local point1,point2 = (Part1.CFrame + Part1.CFrame.LookVector),(Part1.CFrame + Part1.CFrame.LookVector*-1)
local mag1,mag2 = (point1.Position-Part2.Position).Magnitude,(point2.Position-Part2.Position).Magnitude
return not (mag1 <= mag2)
end
while true do
if IsBehind(main, part) then
print(true)
else
print(false)
end
wait(1)
end