Create maximum angle for cannon


How would I add a maximum rotation angle for this gun?

It uses CFrame to pivot the cannon, but I cannot figure out how to add a maximum rotation to it, so it doesnt aim > 80 degrees upwards

1 Like

You’ll probably want to use math.clamp.
Here: math | Roblox Creator Documentation

1 Like

Yeah I have tried that, the problem is. It uses CFrame to lookat a target (part), so it is looking at the parts position and I have no idea how to convert that into an angle to clamp it

Can I see the code that handles that?

	local RotateModel = script.Parent.Vertical
	local topivot = RotateModel

	local targetpos = game.Workspace:WaitForChild('Focus').Position

	local horizontalCframe = CFrame.new(topivot:GetPivot().p,	Vector3.new(targetpos.X, targetpos.Y, 	targetpos.Z))

	script.Parent.Vertical:PivotTo(horizontalCframe)

1 Like

You’ll have to clamp either the X or Z. Something like this:

local horizontalCframe = CFrame.new(topivot:GetPivot().p,	Vector3.new(math.clamp(targetpos.X, 0, 80), targetpos.Y, 	targetpos.Z))

I know referenced same things multiple times, but this is just a test script so its just bodged together quickly

1 Like

Got some pretty weird behaviour

Need to get an angle from the gun to the target but im not sure how to calculate it

Not sure as well, I’m afraid I’ll be of no help for this problem. But is the problem mentioned in the original post solved?

EDIT: Think I found a post for this:

Yes that works, but not in my case, the problem is I cannot check what the angle will be before the weapon is already orientated towards it, so :confused:

Yet again this problem comes up,

TL;DR,

Use :ToOrientation to convert CFrame to angles then clamp those angles than make the CFrame again using CFrame.fromOrientation(x,y,z).

More complicated use to object space to make sure the angles are relative to the base of the turret and such which my module accounts for:

2 Likes