Need help with a side scrolling shooter

I’m having a bit of trouble with the game I’m developing. I want 360 degree shooting, but I’m having trouble getting the gun to look behind the character without getting caught in a euler lock or just not working. Right Now what I’m doing is:

MS = MousePosition.transform.position; //Get Mouse Position

newRotation = Quaternion.LookRotation(MS).eulerAngles; //Look At Mouse
newRotation.x = -newRotation.x;
newRotation.y = 90;
newRotation.z = 0;					//Insure proper rotations
transform.rotation = Quaternion.Euler(newRotation); //Rotate

Mouse Position being a cube I mapped to the mouse position. What happens here is the gun rotates in front of the player semi-accurately, but if I put the mouse behind the player the gun just looks forward but aiming up or down at the mouse. The other problem is when I jump the gun stops working altogether, not being able to aim down at all.

I’ve tried to find tutorials where someone does what I’m trying to do but I’ve come up dry, can someone help?

Well I’m going to provide the answer then, if anyone is having trouble getting a 2D side scrolling shooter to have a gun that rotates 360 degrees to follow the mouse (Did I get all the possible google searches with that) The following code works:

This code is applied to an empty gameobject that follows the mouse. NOTE: I’m using z/y instead of x/y for my game.

 void Update () {
            //Z = X / Y = Y

		//Set this to World Coordinates of mouse
		MS = Camera.mainCamera.ScreenToWorldPoint(Input.mousePosition);
		transform.position = new Vector3(0, MS.y, MS.z);
		
		//Set Values for Vector2
		GetValues ();
      
		//A: Mouse Position
		//B: Right Angle Point
		//C: Control Point
        //D: Inverse Angle
		//E: Angle to rotate gun at
		//F: Player Position
		
		//      A
		//      |\\
		//      |D\E\
		//      B--F-C
		
		//GET RIGHT ANGLE TRIANGLE TO FIND VALUE A
		float adj = Vector2.Distance(PlayerPos, RightAngle);
		float opp = Vector2.Distance(MousePosition, RightAngle);
		AngleA = Mathf.Rad2Deg*Mathf.Atan(opp/adj);

		//Derive Theta
		if(MousePosition.y > PlayerPos.y){
			if(MousePosition.x > PlayerPos.y){
				Theta = 180 - AngleA;
			}
			else
			{
				Theta = 0 + AngleA;
			}
		}
		else{
			if(MousePosition.x > PlayerPos.x){
				Theta = 180 + AngleA;
			}    
			else
			{
				Theta = 0 - AngleA;
			}
		}
	}
	
	void GetValues(){
		PlayerPos = Vector2.zero;
		
		ControlPoint = new Vector2(PlayerPos.x + 1, PlayerPos.y);
     		
		MousePosition = Camera.mainCamera.WorldToViewportPoint(MS);
               //Be in the right place!!
		MousePosition.x -= 0.5f;
		MousePosition.x *= -1f;
		MousePosition.y -= 0.5f;
	
		
		RightAngle = new Vector2(MousePosition.x, PlayerPos.y);
			
		
		//Control Point = Green/Red
		//Player = Grey/Purple/Red
		//Right Angle = Yellow/Grey
		//Mouse Position = Green/Yellow/Purple		

                //Uncomment for some visualization
		/*Debug.DrawLine(RightAngle, MousePosition, Color.yellow);
		Debug.DrawLine(MousePosition, PlayerPos, Color.magenta);
		Debug.DrawLine(ControlPoint, MousePosition, Color.green);
		Debug.DrawLine(PlayerPos, ControlPoint, Color.red);
		Debug.DrawLine(PlayerPos, RightAngle, Color.grey);*/
	}

Then on the gun itself place

		transform.rotation = Quaternion.Euler(new Vector3(Theta, 0,0));

Where theta is the value returned from the Mouse GameObject

If anyone wants to pitch in and clean this up feel free.