# Rotating A 2D Cannon Based On Mouse Position [duplicate]

This question already has an answer here:

I'm creating a game like bubble shooter, I have a cannon which is in the image bellow:

I want to rotate the cannon based on mouse position.

So far I tried various solutions I found over the web but non of them are working, I know that I only need to rotate the cannon on the Z axis because this is a 2D game, so I don't touch X and Y axis.

Does anybody know how can I rotate the cannon based on mouse position?

Or at least provide me a sample script on where to start.

Thank You.

## marked as duplicate by MichaelHouse♦Aug 28 '15 at 13:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

## 1 Answer

You need to create a vector pointing towards the mouse -> Subtract the position of your cannon from the position of your mouse.

vector2 dir = mouse.position-cannon.position


Then you get the angle from this vector by using this formula if y is up and x is right

angle = arctan(dir.y/dir.x) // or use atan2(y,x)


Now you have the total angle that you need to use for you cannon.

Keep in mind that you may need to translate from radians to degrees. You may also need to offset the angle by 90 degrees or so depending on where 0 degrees are in your coordinate system.

• you answer did help me to start, but the problem is that the cannon acts like crazy it does not follow the mouse movement slowly it just goes really fast, and when I move the mouse on the left side it does not follow the same way as it follows when I move the mouse on the right side. I'll post the code in the next message, keep in mind that I experimented a little with the code, first I tried without all if else statements in the code and after that I added those statements – Fahir M Aug 28 '15 at 16:32
• void MoveMachineMouse() { Vector2 dir = Input.mousePosition - transform.position; float angle = Mathf.Tan (dir.y / dir.x) * Mathf.Rad2Deg - 90f; // if (angle > 80) { // angle = 80; // } else if (angle < -80) { // angle = -80; // } if (angle < 80f) { angle = 80f; } else if (angle > 270f) { angle = 270f; } transform.rotation = Quaternion.Euler (new Vector3(0, 0, angle)); } – Fahir M Aug 28 '15 at 16:33
• are sure about *.Tan(...)? you need to use arctan(dir.y/dir.x), or something equivalent – VaTTeRGeR Aug 28 '15 at 16:53
• thanks, atan2 - 90 worked perfectly, but now I have two more issue that I hope that you can help me with, you see the picture above where the cannon is, well I don't want to rotate the cannon bellow the green line, not the green stuff in the background but the green rectangle where the cannon is standing, I don't want it going bellow that rectangle on the right or left side... do you know how can I set boundaries so to say? and also it follows the mouse even though the mouse is not moving on the actual gameplay screen, the mouse is moving over whole unity editor and the script follows it – Fahir M Aug 28 '15 at 17:12
• do you have any idea how can I not follow the mouse in the whole editor and just on gameplay? and also regarding the first issue... and thanks a lot for your help so far!!! – Fahir M Aug 28 '15 at 17:13