# Rotate object/weapon towards mouse cursor 2d.

I wouldn't think this to be difficult, but apparently it is. I've tried multiple functions (LookAt, Rotate, etc.) and cannot figure out the correct order to get my weapons to always point towards mouse cursor. It is a 2d game, side view shooter. Very similar to crash commandos http://www.youtube.com/watch?v=4bO80FvD2Xs . Need objects to rotate around the z axis and when mouse is clicked a fires towards cursor. Any help or links would be appreciated.

Here. This should work.

``````var mouse_pos : Vector3;
var target : Transform; //Assign to the object you want to rotate
var object_pos : Vector3;
var angle : float;

function Update ()
{
mouse_pos = Input.mousePosition;
mouse_pos.z = 5.23; //The distance between the camera and object
object_pos = Camera.main.WorldToScreenPoint(target.position);
mouse_pos.x = mouse_pos.x - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(Vector3(0, 0, angle));
}
``````

Basically, I just get the position of the mouse on screen, compare it to the target object, convert that to degrees, and rotate the object around the z axis.

You can do something like this:

``````Vector3 mousePos = Input.mousePosition;
mousePos.z = -(transform.position.x - Camera.mainCamera.transform.position.x);

Vector3 worldPos = Camera.mainCamera.ScreenToWorldPoint(mousePos);

transform.LookAt(worldPos);

``````

Your guns z-axis (transform.forward) will always point in the direction of you mouse pointer on your 2D-Plane.

Explanation: A cameras ScreenToWorldPosition() will transform a point on your screen to a point in the world. As a mousePosition only has to Coordinates (it's moved on a plane...) You have to tell the ScreenToWorldPosition() method "how far into the screen it should look". Or the distance to the plane that you want to project on.

Dusk’s answer workd for me. Here is a c# version that might be useful:

``````	void Update () {
//rotation
Vector3 mousePos = Input.mousePosition;
mousePos.z = 5.23f;

Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;

float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
``````

Thanks for your help, but it did not work :-( The weapon would not rotate at all. Let me post what I have.

function Update () { var mousePos : Vector3 = Input.mousePosition;

mousePos.z = (transform.position.x - Camera.mainCamera.transform.position.x);

var worldPos : Vector3 = Camera.mainCamera.ScreenToWorldPoint(mousePos);

transform.LookAt(worldPos); }

Also copied what you had into C# same thing of course. If anyone has any ideas on how to make it rotate I sure would appreciate it.

I’m using this one in my games, it should work without issues. Apart from having the lookAt2D function, it offers a simple dropdown to choose between X and Y axis and also a bool to easily invert the direction.

Code:

``````using UnityEngine;

public class LookAt2D_v2 : MonoBehaviour
{
public Camera cam;
public enum Axis { x, y }
public Axis axis = Axis.y;
public bool inverted;

private Vector3 mousePosition;
private Vector3 lookAtPosition;

private void Update()
{
if (cam == null)
{
Debug.LogError(gameObject.name + " target missing!");
return;
}
// store mouse pixel coordinates
mousePosition = Input.mousePosition;

// distance in z between this object and the camera
// so it always align with the object
mousePosition.z = -cam.transform.position.z + transform.position.z;

// transform mousePosition from screen pixels to world position
lookAtPosition = cam.ScreenToWorldPoint(mousePosition);

// Calculate normalized direction
Vector2 direction = (lookAtPosition - transform.position).normalized;

Debug.DrawRay(transform.position, direction * 20f, Color.blue);

switch (axis)
{
case Axis.x:
if (!inverted)
transform.right = direction; // Point x axis towards direction
else
transform.right = -direction; // Point x axis towards inverted direction
break;

case Axis.y:
if (!inverted)
transform.up = direction; // Point y axis towards direction
else
transform.up = -direction; // Point y axis towards inverted direction
break;

default:
break;
}
}

private void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(lookAtPosition, 0.2f);
}
}
``````

Im trying to make a topdown shooter in multiplayer but for some reason the line
var point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Does not seem to work. The error given is :
NullReferenceException: Object reference not set to an instance of an object
PlayerController.Update () (at Assets/Scripts/PlayerController.cs:41)
Im using c# btw. Thanks in advance