Stopping arm from rotating backwards

Hi everyone,

I successfully made a “arm follows mouse” system, but I don’t want the arm to go backwards, as it feels unrealistic, like this:

image

Instead, I would prefer if the arm would stop once it reaches 90 degrees to any side. Is there any way to modify my current system of scripts to do that?

Script:

local event = game:GetService("ReplicatedStorage"):WaitForChild("UpdateArmWeld")

event.OnServerEvent:Connect(function(p, TorsoPos, MousePos, armOffset, armWeld, TorsoCF)
	local cframe = CFrame.new(TorsoPos, MousePos) * CFrame.Angles(math.pi/2, 0, 0)
	armWeld.C0 = armOffset * TorsoCF:toObjectSpace(cframe)
end)

Any help is appreciated!

Perhaps make use of the math.clamp() method in order to prevent rotation further than needed?

Do you know how I would integrate that into my current script?

You take the orientation of the arm with arm.Orientation and clamp each of the Vector3 values. Correct me if I’m wrong, but I think you clamp them between -1 and 1.

Like so?

event.OnServerEvent:Connect(function(p, TorsoPos, MousePos, armOffset, armWeld, TorsoCF, arm)
	local cframe = CFrame.new(TorsoPos, MousePos) * CFrame.Angles(math.pi/2, 0, 0)
	arm.Orientation = Vector3.new(math.clamp(-1,1),math.clamp(-1,1),math.clamp(-1,1))
	armWeld.C0 = armOffset * TorsoCF:toObjectSpace(cframe)
end)

Potentially - does this work?
charscharschars

No. I tried this other script, but no constraints happen:

local event = game:GetService("ReplicatedStorage"):WaitForChild("UpdateArmWeld")

event.OnServerEvent:Connect(function(p, TorsoPos, MousePos, armOffset, armWeld, TorsoCF, arm)
	local cframe = CFrame.new(TorsoPos, MousePos) * CFrame.Angles(math.pi/2, 0, 0)
	local x = arm.Orientation.X
	local y = arm.Orientation.Y
	local z = arm.Orientation.Z
	local xyz = Vector3.new(math.clamp(x,-1,1),math.clamp(y,-1,1),math.clamp(z, -1,1))
	armWeld.C0 = armOffset * TorsoCF:toObjectSpace(cframe) * xyz
end)

Then I’m afraid I won’t be of much use. I’m pretty rusty on roblox transformations :skull:

I managed to do it by making a new function to clamp angles, and then I limited the angles to a maximum of 90 degrees.
I’m not sure if it’s the best way, but it works. :slight_smile:

1 Like