# Trying to get the player to face the direction of a touch

I’m having quite the issue with player movement, and I’ve searched for answers but had no luck. So the I have a ship and the ship follows my finger. That works. What I cant seem to get is the ship to rotate and face the direction of the touch. Pretty much all the examples I’ve seen for doing this have been with Mouse input or in 3d games, but I have a touch (and drag) top down 2d game. The top of my ship is what I want to spin around to face my finger but I havent gotten anywhere trying to get a solution. Here is what I have for movement so far…

``````using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour
{

public float speed = 1f;
Vector2 pos;

void Start ()
{

pos = transform.position;

}

void Update ()
{
pos = transform.position;

if (Input.touchCount > 0) {

Vector2 touchPos = Input.GetTouch (0).position;

transform.position = Vector2.MoveTowards (pos, Camera.main.ScreenToWorldPoint (touchPos), speed * Time.deltaTime);

}
``````

}
Any help is appreciated. I’m fairly new to this, so you may have to elaborate… Thanks!

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

Note from robertbu (source): 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;

The touch position is in place of the click on this script.

You have to use some basic trigonometry to figure out what direction face. Trig is typically taught using triangles, but it deals with circles at it’s most fundamental. I’m assuming you’ve heard of the Unit Circle (essentially a circle with a radius of 1 at 0,0). Your touch point is the distance from the origin. The x,y coordinate is essentially the position on this circle. Atan2(arc tangent) takes the distance, in accordance with both x and y position, and returns the associated angle being swept counter-clockwise from (1, 0) on the unit circle that intersects with this position. This is returned in radians, which is why you multiply by Rad2Deg(conversion rate).

I probably missed something, or was slightly off on some bit of information. This should be enough to get you started as this is all from memory and code that I grabbed from said source to save me some time.