Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can I fix this angle problem?

Asked by 9 years ago

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

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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 -.

0
thanks XToonLinkX123 580 — 9y
Ad

Answer this question