How would i go about making this on a local axis

what i mean by that is if i make it move forward by the zaxis and then rotate the model the rotation of the ray doesnt follow

script

local hand = script.Parent.RootPart.rig.joints.body_JNT1.body_JNT2.body_JNT3.body_JNT4.body_JNT5.lf_shoulder_JNT.lf_arm_JNT1.lf_arm_JNT2.lf_arm_JNT3.lf_arm_JNT4.lf_arm_JNT5.lf_arm_JNT6.lf_arm_JNT7.lf_arm_JNT8.lf_arm_JNT9.lf_arm_JNT10
local target = script.Parent.RootPart.LeftIk

local origin = script.Parent.RootPart.LeftRay

while true do
local direction = Vector3.new(0,0,script.Parent.Torso.LeftRay.Rotation.Z + 90)
local ray = workspace:Raycast(origin.Position,direction)
wait()
if ray then
local rayDist = ray.Distance
–local dist = Vector3.new(hand.Position.X - target.Position.X, hand.Position.Z - target.Position.Z, hand.Position.Z - target.Position.Z).Magnitude
print(rayDist)
if rayDist < 28 then
target.CFrame = target.CFrame:Lerp(CFrame.new(ray.Position.X, ray.Position.Y + math.random(-20, 10), ray.Position.Z) * CFrame.Angles(0,0,0),0.3)
end
end
end

Just to make your script readable…

local hand = script.Parent.RootPart.rig.joints.body_JNT1.body_JNT2.body_JNT3.body_JNT4.body_JNT5.lf_shoulder_JNT.lf_arm_JNT1.lf_arm_JNT2.lf_arm_JNT3.lf_arm_JNT4.lf_arm_JNT5.lf_arm_JNT6.lf_arm_JNT7.lf_arm_JNT8.lf_arm_JNT9.lf_arm_JNT10
local target = script.Parent.RootPart.LeftIk

local origin = script.Parent.RootPart.LeftRay

while true do
	local direction = Vector3.new(0,0,script.Parent.Torso.LeftRay.Rotation.Z + 90)
	local ray = workspace:Raycast(origin.Position,direction)
	wait()
	if ray then
		local rayDist = ray.Distance
		local dist = Vector3.new(hand.Position.X - target.Position.X, hand.Position.Z - target.Position.Z, hand.Position.Z - target.Position.Z).Magnitude
		print(rayDist)
		if rayDist < 28 then
			target.CFrame = target.CFrame:Lerp(CFrame.new(ray.Position.X, ray.Position.Y + math.random(-20, 10), ray.Position.Z) * CFrame.Angles(0,0,0),0.3)
		end
	end
end

You should be using CFrame:ToObjectSpace to set a CFrame on a Local Axis.

2 Likes