How Do You Set A Limit On Angle So You Don't Over It

I want to set a limit on bodygyro to where you can point the mouse under you and you dont face the ground.

https://gyazo.com/fae0b8c25f44f6fe6e6a89642ecb7eb6

local Gyro = Instance.new("BodyGyro")
		Gyro.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
		Gyro.P = 200000000
		Gyro.CFrame = CFrame.new(HRP.Position,Mouse.Hit.p)
		Gyro.Parent = HRP
1 Like

I’m assuming you are talking about having the character follow your cursor. I’ve done something similar to this in the past and a simple way to do so is something like this on the Y axis:

local yAxis = (Mouse.Hit.Position - Mouse.Origin.Position).Unit.Y
  if yAxis > 0.25 then
	yAxis = 0.25
  elseif yAxis < -0.25 then
	yAxis = -0.25
  end

do i put that in bodygyro? or keep it seperate

bodygyro takes a rotational matrix iirc so just do like bodygyro.CFrame = CFrame.Angles(0, yAxis, 0)

it doesnt work still
https://gyazo.com/c8929a71ac491e142ae12e450f02a7c9

if Key.KeyCode == Enum.KeyCode.G and Holding == false then
		Holding = true
		local Gyro = Instance.new("BodyGyro")
		Gyro.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
		Gyro.P = 200000000
		Gyro.CFrame = CFrame.new(HRP.Position,Mouse.Hit.p)
		Gyro.Parent = HRP
		while Holding == true do
			local yAxis = (Mouse.Hit.Position - Mouse.Origin.Position).Unit.Y
			if yAxis > 0.25 then
				yAxis = 0.25
			elseif yAxis < -0.25 then
				yAxis = -0.25
			end
			Gyro.CFrame = CFrame.new(HRP.Position,Mouse.Hit.p) * CFrame.Angles(0, yAxis, 0)
			wait()
			if Holding == false then
				Gyro:Destroy()
			end
		end
	end

To limit something’s min and max use math.clamp()

that doesnt solve the problem, and i dont even who where to put it

1 Like

You just replace the value with the math.clamp() with some arguments.
Here’s an explanation of math.clamp that I made a while ago:

1 Like

but how does that solve the problem with it move the whole character instead of rotation left and right and a limited angle up and down

1 Like

The title says “How to set a limit on angle”. I just told you.

can you write like the code out because I dont know how to apply that

1 Like

You could use math.clamp() for it but it is unnecessary in this case.

The issue is that CFrame multiplication works in mysterious ways.

If up and down movement is not required then the code below will work fine.

if Key.KeyCode == Enum.KeyCode.G and Holding == false then
			Holding = true
			Gyro = Instance.new("BodyGyro")
			Gyro.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
			Gyro.P = 200000000
			Gyro.CFrame = CFrame.new(HRP.Position,Mouse.Hit.p)
			Gyro.Parent = HRP
			while Holding == true do
				local GyroFrame = CFrame.new(HRP.Position,Mouse.Hit.p)
				local rX,rY,rZ = GyroFrame:toOrientation()
				Gyro.CFrame = CFrame.new(GyroFrame.Position) * CFrame.fromOrientation(0, rY, rZ)
				wait()
			end
			Gyro:Destroy()
	end
2 Likes

That is exactly what I just said,.