# How can I rotate my player to look at the direction my Joystick is pointing to? (Top-Down 2D)

Top-Down 2D:

How can I rotate my player to look at the direction my Joystick is pointing to?

Here is the code that I’m using for my Joystick.
It moves my tank properly but at the same rotation (as expected, since I have not coded it yet to rotate/look at the position I’m moving towards to)

``````Vector3 tempJoyDelta;
public static Vector3 joyDelta;
Vector3 touchPosition;
public Transform centerPos;
Ray ray;
int currTouch = 64;
bool moving;
float saucerSpeed;
public GameObject tank;

void Awake () {

speed = 1.0f;

}

void Update () {

if(Input.touchCount > 0)
{
for(int i = 0; i < Input.touchCount; i++)
{
currTouch = i;
ray = Camera.main.ScreenPointToRay(Input.touches*.position);*
``````
• `````` 		if(Physics.Raycast(ray, Mathf.Infinity, sprintMask))*
``````
• `````` 		{*
``````
• `````` 			if(Input.GetTouch(currTouch).phase == TouchPhase.Began && !moving)*
``````
• `````` 			{*
``````
• `````` 				moving = true;*
``````
• `````` 			}*
``````
• `````` 			if(Input.GetTouch(i).phase == TouchPhase.Ended || Input.GetTouch(i).phase == TouchPhase.Canceled)*
``````
• `````` 			{*
``````
• `````` 				moving = false;*
``````
• `````` 			}*
``````
• `````` 			if(Input.GetTouch(i).phase == TouchPhase.Moved || Input.GetTouch(i).phase == TouchPhase.Stationary)*
``````
• `````` 			{*
``````
• `````` 				touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(i).position.x, Input.GetTouch(i).position.y, 0));*
``````
• `````` 			}*
``````
• `````` 		}*
``````
• `````` 	}*
``````
• `````` }*
``````
• `````` if(moving)*
``````
• `````` {*
``````
• `````` 	joyDelta = centerPos.position - touchPosition;*
``````
• `````` 	joyDelta.z = 0;*
``````

_ tank.transform.Translate(((Vector3.forward * joyDelta.y) * speed) * Time.deltaTime);_
_ tank.transform.Translate(((Vector3.right * joyDelta.x) * speed) * Time.deltaTime);_

• `````` }*
``````

}
Thanks!

Take the vector at which joystick is pointed and use math to get heading:

``````float heading = Mathf.Atan2(joyvector.x,joyvector.y);
``````

It is heading in radians which can be used to point the object in right direction like:

``````transform.rotation=Quaternion.Euler(0f,0f,heading*Mathf.Rad2Deg);
``````

if (moveVec.sqrMagnitude > 0.1) {
angle = Mathf.Atan2 (moveVec.x, moveVec.y) * Mathf.Rad2Deg;

``````		transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.Euler (new Vector3 (0, 0, -angle)), Time.deltaTime * rotationSpeed);
}
``````

Thanks, You have solved a problem that I’ve had for days.

@meticodetest Just use interpolation between angles this should smooth it out for you. il get an example up as soon as i can