# 2D Directional Movement

Discussion in '2D' started by emretufekci, Aug 13, 2019.

1. ### emretufekci

Joined:
Apr 11, 2019
Posts:
3
I would like to create an arrow as a player. When i click up button / or tap on the mobile screen it should go forward according it's on direction. But it's not. I tried something but couldn't find the answer.

This video shows what i want to create:

By the way, arrow is rotates around itself i didn't show this in the video.
How can i do this ?

2. ### LiterallyJeff

Joined:
Jan 21, 2015
Posts:
2,768
This has been asked and answered countless times. Make sure you're googling for answers, this is an easy one to find.

I'm much happier to help you fix the code you already wrote rather than give you new code that you don't fully understand.

3. ### emretufekci

Joined:
Apr 11, 2019
Posts:
3
I'm googling during two days, actually video is not showing clearly the hardest point is arrow is spinning.

This video is much clear:

Code (CSharp):
1.
2.                   float angle = transform.eulerAngles.z * Mathf.Deg2Rad;
3.                    float sin = Mathf.Sin(angle);
4.                    float cos = Mathf.Cos(angle);
5.
6.                    Vector3 forward = new Vector3(
7.                        (direction.x * cos - direction.y * sin) ,
8.                        direction.x * sin + direction.y * cos ,
9.                        1f);
10.
11.                    Vector3 position = transform.position;
12.
13.
14.
15.                    Debug.DrawLine(position, position + direction, Color.red);
16.                    Debug.DrawLine(position, position + forward, Color.green);
17.

This code is so close to my achieve, green part is exactly matches with my arrow. i would like go forward.
I've taken this code from somewhere idk.

4. ### LiterallyJeff

Joined:
Jan 21, 2015
Posts:
2,768
Your arrow is pointing along the -X axis, so you can move it in that direction like this:
``transform.position += -1 * transform.right * speed;``

or like this:
``transform.Translate(Vector3.left * speed, Space.Self);``

That will make your arrow move in the negative X direction according to its rotation.

emretufekci likes this.
5. ### emretufekci

Joined:
Apr 11, 2019
Posts:
3
Thank you so much this code is working. I was looking for it. U saved tons of my time thank you

NOTE for newbies (like me): Who is looking this, when u try this code if object dissappears try like this:

transform.position += -1 * transform.right * 1f;

unityunity