Limiting Constraint Angle

Hello, I’m trying to make the arm point towards the mouse. Is there way to make it so it doesn’t go fully 360 degrees and have the actual angle of the arm also face the same direction.

So like if I move my mouse at a 45 degree angle the arm will also be 45 degrees.

Here’s my arm script:

local cframe = CFrame.new(jointPosition.Position, mouse.Hit.Position) * CFrame.Angles(math.pi/2, 0, 0) * CFrame.new(0.5, -1, -1)

local jointPosition = ArmMotor6D.Part0.CFrame:toWorldSpace(CFrame.new(0.9,0.25,0.25))
ArmMotor6D.C0 = ArmMotor6D.C0:Lerp(ArmMotor6D.Part0.CFrame:toObjectSpace(cframe), 0.2)

So when I use this the arm can go in a full 360 I don’t want that.

Thank you.

1 Like

try this

local jointPosition = ArmMotor6D.Part0.CFrame:toWorldSpace(CFrame.new(0.9, 0.25, 0.25))
local maxAngle = math.rad(45) --change 45 to the maximum angle you want

local desiredCFrame = CFrame.new(jointPosition.Position, mouse.Hit.Position) * CFrame.Angles(math.pi / 2, 0, 0) * CFrame.new(0.5, -1, -1)
local clampedCFrame = desiredCFrame:pointToWorldSpace(jointPosition.Position):pointToWorldSpaceAngles(Vector3.new(math.min(maxAngle, desiredCFrame:toEulerAnglesXYZ())))
ArmMotor6D.C0 = ArmMotor6D.C0:Lerp(ArmMotor6D.Part0.CFrame:toObjectSpace(clampedCFrame), 0.2)

I got this error:

pointToWorldSpaceAngles is not a valid member of Vector3
local jointPosition = ArmMotor6D.Part0.CFrame:toWorldSpace(CFrame.new(0.9, 0.25, 0.25))
local maxAngle = math.rad(45)

local desiredCFrame = CFrame.new(jointPosition.Position, mouse.Hit.Position) * CFrame.Angles(math.pi / 2, 0, 0) * CFrame.new(0.5, -1, -1)
local currentCFrame = ArmMotor6D.C0:pointToWorldSpace(jointPosition.Position)
local clampedCFrame = CFrame.new(currentCFrame.Position, currentCFrame.Position + currentCFrame.LookVector:pointToWorldSpace(desiredCFrame.LookVector):lerp(Vector3.new(), math.sin(maxAngle)))

ArmMotor6D.C0 = ArmMotor6D.C0:Lerp(ArmMotor6D.Part0.CFrame:toObjectSpace(clampedCFrame), 0.2)

try this

1 Like

I got this error:

Position is not a valid member of Vector3

It’s because currentCFrame is not a cframe, it’s a Vector3. So then I put a CFrame around current CFrame and got this error:

toObjectSpace is not a valid member of Vector3

yea i asked ai to get what you wanted thats why there are so many errors, i thought it would find a quick way to do what you needed but i guess not :sob: :pray:

1 Like

Bump. The arms still can go backwards and turn you into a helicopter.

1 Like

What is the jointPosition being used for exactly because you have it defined but your not using it though

@SilentSuprion

Try this and tell me if it works or not

@SilentSuprion

local cframe = CFrame.new(jointPosition.Position, mouse.Hit.Position) * CFrame.Angles(math.pi/2, 0, 0) * CFrame.new(0.5, -1, -1)
local objectspace : CFrame = ArmMotor6D.Part0.CFrame:ToObjectSpace(cframe)
local x,y,z = objectspace:ToOrientation()
x = math.clamp(x, math.rad(-45), math.rad(45))
y = math.clamp(y, math.rad(-45), math.rad(45))
z = math.clamp(z, math.rad(-45), math.rad(45))
local finalCFrame = CFrame.new(objectspace.Position) * CFrame.fromOrientation(x,y,z)
ArmMotor6D.C0 = ArmMotor6D.C0:Lerp(finalCFrame, 0.2)

It moves around in 360 degrees. Here’s the full script:

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

local lastMouseX, lastMouseY = 0, 0

local function updateArm()
	local character = script.Parent
	local rightArm = character:FindFirstChild("Right Arm")
	local torso = character:FindFirstChild("Torso")
	
	if rightArm then
		local ArmMotor6D = torso:WaitForChild("Right Shoulder")

		local jointPosition = ArmMotor6D.Part0.CFrame:toWorldSpace(CFrame.new(0.9,0.25,0.25))
				
		local cframe = CFrame.new(jointPosition.Position, mouse.Hit.Position) * CFrame.Angles(math.pi/2, 0, 0) * CFrame.new(0.5, -1, -1)
		
		local cframe = CFrame.new(jointPosition.Position, mouse.Hit.Position) * CFrame.Angles(math.pi/2, 0, 0) * CFrame.new(0.5, -1, -1)
		local objectspace = ArmMotor6D.Part0.CFrame:ToObjectSpace(cframe)
		local x,y,z = objectspace:ToOrientation()
		x = math.clamp(x, math.rad(-45), math.rad(45))
		y = math.clamp(y, math.rad(-45), math.rad(45))
		z = math.clamp(z, math.rad(-45), math.rad(45))
		local finalCFrame = CFrame.new(objectspace.Position) * CFrame.fromOrientation(x,y,z)
		ArmMotor6D.C0 = ArmMotor6D.C0:Lerp(finalCFrame, 0.2)
		
	end
end

mouse.Move:Connect(updateArm)

So you’re trying to make the arm point at where the the mouse is if im understanding correctly?

1 Like

Yes, it works perfectly except you can move your arm around in 360 degrees when you put the mouse behind your character. So that’s why I need help.

I just tested it out and it looks like its working pretty janky, like minus the being able to turn 360 degrees

1 Like

Alright I whipped up a different script for you that actually correctly points to the mouse and doesn’t look janky!

I added clamps but currently as you can see they go 360 for every axis, you’ll have to adjust it on your own to get the feel of clamping you want

@SilentSuprion

local player = game:GetService("Players").LocalPlayer
local TweenService = game:GetService("TweenService")
local mouse = player:GetMouse()
local camera = workspace.CurrentCamera

local function updateArm()
	local character = script.Parent
	local rightArm = character:FindFirstChild("Right Arm")
	local torso = character:FindFirstChild("Torso")
    local motor : Motor6D  = torso:WaitForChild("Right Shoulder")
    local orgc1 = motor.C1
	
	if rightArm then

      local unitray = camera:ScreenPointToRay(mouse.X, mouse.Y, 1)
      local objectspace = motor.Part0.CFrame:ToObjectSpace(CFrame.lookAt(motor.Part0.Position, motor.Part0.Position + unitray.Direction) * CFrame.Angles(math.rad(90),0,0))
      local x,y,z = objectspace:ToOrientation()
      x = math.clamp(x, math.rad(-360), math.rad(360))
      y = math.clamp(y, math.rad(-360), math.rad(360))
      z = math.clamp(z, math.rad(-360), math.rad(360))

      local finalCFrame = CFrame.new(motor.C0.Position) * CFrame.fromOrientation(x,y,z) * orgc1.Rotation
      local t = TweenService:Create(motor, TweenInfo.new(.45, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {
         C0 = finalCFrame
      })
      t:Play()
	end
end

mouse.Move:Connect(updateArm)

3 Likes

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