Set a limit to orientation of a part

I get the players mouse locally and then fires a event to a canon that the player is steering and i set the canon model to the players mouse cframe so that the canon points in the direction the mouse is pointing. The canon is working but i would like to set an max orientation as now you can spin the canon if you for an example point the mouse backwards and i want to limit the X and Y orientation so that the canon will not move past those max limits. Any ideas?

Try using math.clamp, it returns a value between a set min and max.
math.clamp(value, min, max)

For example:
math.clamp(2, 1, 5)
This would return 2. This is because 2 is above the 1 minimum and is under the 5 maximum.

math.clamp(12, 3, 9)
This would return 9. This is because 12 is above the 3 minimum but exceeds the 9 maximum. When it exceeds something it just sets it to the max.

I have no clue how your orientation math is done. But it would probably be easiest to clamp each value of the orientation individually.

Here is a quick example:

local rawOrientation = -- your unclamped orientation

local x = math.clamp(rawOrientation.X, min, max) -- min, max is placeholder
local y = math.clamp(rawOrientation.Y, min, max)
local z = math.clamp(rawOrientation.Z, min, max)
local orientation = Vector3.new(x,y,z)

This could actually help thanks but i just realized that my standard orientation Y of the canon is “-180” and when i move the mouse little bit to the right the value goes from “-180” which is a negative value to “180”… so as i will have a lot of canons and they will probably be rotated that means that they have to adapt after the “standard” orientation and calculate a custom min and max orientation for every single canon on its own… but i cant figure out the -180 and 180 part, otherwise i could just check if the canon orientation Y is >= “LeftMaxY” and <= “RightMaxY”

I’m not certain if this will help, but you could try using a little circle math to fix this.

When you have a negative value within a circle, you can always add 360 to get it’s positive counterpart.

local function ThreeSixty(x)
	if x < 0 then -- if x is negative
		return x + 360 -- add 360
	end
end

local newX = ThreeSixty(-20) -- returns 340

This will convert all -180 to 180 orienations into 0 to 360 measurements.
So just plug newX into the math.clamp function instead. No more dealing with negatives.

Nothing so far have worked, still on the same problem… :frowning:

hey have you fixed it? 30chaaar