Firing at mouse position, 2d game

Hello, I am writing my second question here in hopes for some guidance.

Currently I am working on a side scrolling game. I would like to be able to accurately fire at the mouse cursor on screen. I have attached a script to my “gun” on my player’s ship that will aim at the mouse pointer however it is not accurate. The current script just changes where the gun points based on mouse input and will not react to the actual mouse position.

An example…ship is at top left of the screen, mouse is pointing at the middle of the screen. If the ship is moved down while the mouse cursor remains in the center of the screen, the gun will not look at the mouse cursor. I would like the script to always have the gun look at the mouse pointer.

I imagine the solution involves drawing a ray from the camera to the mouse position in world space, getting the Vector3 coords of that RaycastHit and storing it. However this is where I get lost. I’m not sure how to rotate an object to look at a Vector3 coordinate. I have seen the Transform.LookAt function and feel my answer lies there, but is it that simple?

var minimumY : float = -60;
var maximumY : float = 60;
var rotationY : float = 0;
var originalRotation : Quaternion;

static var rapidFire : boolean = false;

var bullet : Rigidbody;
var rapidfireBullet : Rigidbody;
var specialBullet : Rigidbody;
var explosion : Transform;

function Update(){
    rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    rotationY = ClampAngle (rotationY, minimumY, maximumY);
    yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.forward);
    transform.localRotation = originalRotation * yQuaternion;
    //looks to see if left mouse button is prssed, if it is, fires a bullet
		if(rapidFire == true){
			var rapidfiretempBullet : Rigidbody;			
			rapidfiretempBullet = Instantiate(rapidfireBullet, transform.position, transform.rotation);
		var tempBullet : Rigidbody;			
		tempBullet = Instantiate(bullet, transform.position, transform.rotation);
	//right click for special attack
		if(playerScript.playerFormPoints > 10){
			var tempSpecialBullet : Rigidbody;

			tempSpecialBullet = Instantiate(specialBullet, transform.position, transform.rotation);
			playerScript.playerFormPoints -= 3;
function Start(){
    	rigidbody.freezeRotation = true;
    originalRotation = transform.localRotation;

function RapidfireTimer(){
	yield new WaitForSeconds(10);
	rapidFire = false;
	Debug.Log("Rapid Fire is false");
function ClampAngle (angle : float, min : float, max : float){
    if(angle < -360){
    	angle += 360;
    if(angle > 360){
    	angle -= 360;
    return Mathf.Clamp(angle, min, max);

Are you moving the camera position as well?

You can use Input.mousePosition to get the location of your mouse on each frame.

var relative = gun.position - Input.mousePosition;
gun.rotation = Quaternion.Euler(0,0, Mathf.Atan2(relative.y, relative.x) * Mathf.Rad2Deg);

You’ll have to align your coordinate systems with this though.

Thank YOU!

This code works perfectly!!

function Update () {
    var mousePos = Input.mousePosition;
    mousePos.z = 32; //The distance from the camera to the player object
    var lookPos : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
    lookPos = lookPos - transform.position;
    var angle : float = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);


Wish I searched a little harder because this has been asked before!