- Home /
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!
Answer by Aggrojag · Nov 30, 2015 at 04:02 AM
Vector2 dir = touchPos - transform.position;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
Source(http://answers.unity3d.com/questions/654222/make-sprite-look-at-vector2-in-unity-2d-1.html)
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.
Thanks so much for the help! This script works really well but I have one more question. How would I go about making this rotate at a certain speed. Right now to points directly at my finger, but can I make it spin to face my finger not so...automatically i guess?
You would treat the angle variable as where you end up facing. Then you would figure out if you need to rotate left or right (shortest distance). From there you would increase/decrease your current angle until it's within some acceptable range (say +-.5f[if(curAngle >= angle -.5f && curAngle <= angle +.5f)]) that way it doesn't have to be exact, as most times you'll find dealing in exact numbers is bad when it comes to float. Then you're done. Probably wont post an example script as this should be doable at your level of understanding (though possibly difficult). Just break it down in to easy to accomplish bits of code, and you'll do fine. Cheers.
I'll try this tomorrow then post back with what I can get. Thanks for all of your help!