Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I fix this angle problem?

Hello I have a problem with the angle, actually I know what's the problem but I can't fix it.

I'm trying to set the right angle from the position of where it shoot and the position of my mouse, but sometime the bullet get the bad angle.

My problem is right on this little gif.

*IMPORTANT: * the variable dy is the distance of the axe of y and the variable distance is the hypotenuse from where I click

there's a part of my code where I get the problem:

function bullet:SetRotation(defAngle)
	local angle = defAngle or math.deg(math.asin(self.data.dy / self.data.distance))

	self.instance.Rotation = angle --instance is the bullet(frame)
end

1 answer

✓ Accepted answer

Use math.atan2 instead of math.asin.

The trouble is that given only one number, it's always unclear whether or not you want a + or - angle. math.atan2 is given both the dx and dy, so it knows which one is that one you want.

It's usage should be

local angle = defAngle or math.deg( math.atan2(self.data.dy, self.data.dx) )

though you might have to flip the dx and dy or tack on a -.

1 comment

0

thanks

This discussion is preserved as it was. New answers, comments, and votes are closed.