(URGENT!) How can I make this code work in any direction?

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

1 Like

try this

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

Did not work. Just made it only work properly when looking to the right of the world

Try this:

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

Check if the hand is behind or not. if true then increase the spline value if false then decrease spline value.

it looks like you’re using world space cframe. you’ve got to convert to object space cframe of the gun.

I tried doing that with this:

local newHand = CFrame.new(origin.X,origin.Y,hand.Z):ToObjectSpace(LeftObject.CFrame)

But it now I have to reach extremely far in the wrong direction to even move it a little bit:

try doing something like

origin:PointToObjectSpace(handPos )

This worked perfectly!
I added a new variable called hand2 and changed newHand to hand2:

local hand2 = origin:PointToObjectSpace(handPos)+origin.Position

2 Likes

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