Rotating a Sprite to face mouse / target?

EDIT: For anyone visiting this question: this was typed before Unity had any 2D tools so this might not be suitable for your 2D projects, or it might be an overly complicated way of dealing with the problem. Munchy2007’s answer should work fine in most cases.

I’ve hit a wall here as I switched my very early 2d topdown shooter prototype from a 3D project to a 2D one, and started working with 2D sprites instead of planes with images.

I thought this would be a simple switch, but I’m having some problems getting the sprites to rotate properly. After searching and trying multiple solutions, I still get no results.

My game has Raycast Mouse look:

		// Raycast Mouse Looking
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit hit = new RaycastHit();
		if (Physics.Raycast (ray, out hit, 100)) {
			Vector3 hitPoint = hit.point;

			Vector3 targetDir = hitPoint - transform.position;

			targetDir = new Vector3(targetDir.y, targetDir.x, 0); //I tried multiple different setups here, like (0, 0, targetDir.z) etc...

			transform.LookAt(targetDir);

As you might see here, I’m trying to use LookAt() function to make the player Sprite rotate towards the Mouse position. This worked fine with 3D planes, but things got awry when I switched to the 2D settings and 2D View.

Two problems:
Instead of rotating around one axis, the Sprite rotates the whole GAMEOBJECT, making the sprite warp and turn around like it was on a plane (or a 3D object).

On the other hand, if I get the sprite to stay facing towards the screen and turn around, the sprite turns around in a weird manner, and resets the rotation at certain points when moving the mouse around the sprite. It’s like the sprite “Flips” without me ordering it to do so.

I want the sprite to rotate around the Z-AXIS, as in the default Unity 2D view Y is vertical and X is horizontal.

Any solutions? I’m guessing I’m making a very simple mistake somewhere…

Thanks!

I use this to make my sprite follow the mouse. The sprite default orientation is pointing upwards.

void Update () {
		Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
		transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
	}

Answering my own question. I combined the two solutions up there, and ended up with this:

		// Raycast Mouse Looking
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit hit = new RaycastHit();
		if (Physics.Raycast (ray, out hit, 100)) {
			Vector3 hitPoint = hit.point;

			Vector3 targetDir = hitPoint - transform.position;

			float angle = Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg;
			transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
		}

And this seems to be working. The sprite is still rotated 90 degrees clockwise, so facing sideways (the image itself has the character facing down). Any way to fix this other than editing the picture? I like drawing my characters facing up & down… :slight_smile: