- Home /
Tarnsfer direction
Hi, i am writting mobile game. I am using penelope joystick. My problem : I can't find way to transfer direction from joystick to character. I have 2x Vector2 (start,end) of joystick movment. (start for default position, end for position after movment) I need to get direction of this, and use it to set direction of my character.
For example: If start is (30,30) and end is (50,50) i want my character to rotate to north-east My character is 3D
Answer by Shar1ngan · Aug 01, 2013 at 07:28 AM
You need to use Normal Vector.
Vector3 dir = (EndVector - StartVector).normalize;
I did it. But how should i transfer it to my character? By moveTowards, rotate,rotation,eulerAngel,Quaternion?
You can check joystick coordinates, if x: 1, y: 0 then player look (and move) right, if x:0, y: -1, player look (and move) back. You can move player by transform.Translate or if you have rigidbody - rigidbody.AddForce
I wanted to do it by go forward, and only change direction to direction of joystick. But i don't know what should i use on player.transform to give him that Vector3 dir
Answer by robertbu · Aug 01, 2013 at 01:58 PM
There are a couple of ways I can think of to transfer it your character.
var dir = end - start;
var look = Vector3.zero;
look.x = dir.x;
look.z = dir.y;
transform.rotation = Quaternion.LookRotation(look);
And you can use Atan2:
var dir = end - start;
angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0.0, angle + 90.0, 0.0);
I'm not sure of the +90...may be -90. Both of these assume a standard setup with the character walking around on the XZ plane with Y being up.