Make sprite look at vector2 in Unity 2D?

I’m working on a top-down 2D game but can’t quite get a sprite to rotate towards any given vector. So far this is the best code I’ve got for it but the sprite will not look to the left side of the it, only the right:

		transform.rotation = new Quaternion(0,0,transform.rotation.z, transform.rotation.w);

WorldPos is the point I’m getting from ScreenToWorldPoint. I also have to force the rotation for x and y to 0 so that you can still see the sprite. Anybody got anything better for rotating a sprite toward an object in 2D? I’m thinking it should be this hard.

Here something better. I’ve posted it several times, but as usual searching for the answers is not turning up the ones I want:

var dir = WorldPos - transform.position;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

Note this code assumes that the ‘forward’ side of your sprite is the right side. If it is another side (like up), you will need to adjust the angle (subtract 90) before passing it to the AngleAxis() function;

I have enemy ships in a top down shooting game with very similar behavior. The following is a script that can be attached to a GameObject. It has a public variable for the target your are wanting your object to face, and the update behavior sets the rotation.

using UnityEngine;
using System.Collections;

public class FaceTarget : MonoBehaviour
	public Transform target;
	private Vector3 v_diff;
	private float atan2;

	void Update()
		v_diff = (target.position - transform.position);	
		atan2 = Mathf.Atan2 ( v_diff.y, v_diff.x );
		transform.rotation = Quaternion.Euler(0f, 0f, atan2 * Mathf.Rad2Deg );

You can programatically add this component and set its target variable as well. If your object is rotated already, you will have to add or subtract an offset from atan2 to compensate for the objects initial rotation.

There is a simpler and cleaner way to do this than in “Best answer”:

`Vector3 relativeTarget = (target.position - transform.position).normalized;
//Vector3.right if you have a sprite rotated in the right direction
Quaternion toQuaternion = Quaternion.FromToRotation(Vector3.right, relativeTarget);
transform.rotation = Quaternion.Slerp(transform.rotation, toQuaternion, rotationSpeed * Time.deltaTime);`

Try this

using UnityEngine;
using System.Collections;

public class JumpScript : MonoBehaviour {

    public int playerSpeed = 5;
private float rotationx;
private float rotationy;
private Vector3 touchcoordinates;
private Transform myTrans;
private bool RestartButton_Bool;
public Transform background_map;
public Touch touch1;
public GameObject GameoverText;

void Start ()
	//Caching of the variables
	myTrans = this.transform;

// Update is called once per frame
void Update () 
	//Keep the character without any rotation
	myTrans.rotation = Quaternion.Euler(0,0,0);
	//Check to see if the app is running over iOS or Android Devices
	if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android)
		//Get touch data
		foreach (Touch touch in Input.touches) 
			touch1 = touch;
			touchcoordinates = touch.position;
			//Coverting touch coordinates in accordance with game use.
			Ray ray = Camera.main.ScreenPointToRay(touchcoordinates); 

		touchcoordinates = touch1.position;

	//Check if the app is ruuning anywhere other than Mobile devices
		//Coverting touch coordinates in accordance with game use.
		Ray ray1 = Camera.main.ScreenPointToRay(Input.mousePosition);
	//Moving the character forward
	transform.Translate(Vector2.up * Time.deltaTime * 5);


There’s a dph blog post explaining a few different ways to do the 2D LookAt. I think the simplest way is this:


(where ‘direction’ is a vector pointing in whatever direction you want the sprite to face.)

i’ve edited Kame Sama code above :

public Transform target;

	 enum FacingDirection {
		UP = 270,
		DOWN = 90,
		LEFT = 180,
		RIGHT = 0
	void Update(){



	void LookAt2D(Transform theTarget, float theSpeed, FacingDirection facing) {
				Vector3 vectorToTarget = theTarget.position - transform.position;
				float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
				angle -= (float)facing;
				Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
				transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * theSpeed);

You can do it in more elegant way:

transform.rotation = Quaternion.LookRotation (Vector3.forward, (WorldPos - transform.position).normalized);

I think this is the best one…
insert the tag of the object that u want to look at
and play with the offset

   public string Tag;
    public float offset;
            private Transform target;
            private Vector3 targetPos;
            private Vector3 thisPos;
            private float angle;
         void Start () 
                target = GameObject.FindGameObjectWithTag(Tag).GetComponent<Transform>();
         void LateUpdate()
                targetPos = target.position;
                thisPos = transform.position;
                targetPos.x = targetPos.x - thisPos.x;
                targetPos.y = targetPos.y - thisPos.y;
                angle = Mathf.Atan2(targetPos.y, targetPos.x) * Mathf.Rad2Deg;
                transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle + offset));