# Top Down 2D movement script adaption

Hey

I’v been working on top down 2D RTS and found a great youtube tutorial for 3D RTS detailing movement selection and movement.

The selection script I adapted fine, but the movement script has me stumped and don’t have a lot of idea on how to adapt it to an x-y plane, rather then x-z. Any help would be greatly appreciated.

Here’s the video:

Here’s the script he’s written in the tutorial:
Code

``````private void UpdateMove()
{
if (movetoDest != Vector3.zero && transform.position != movetoDest)
{
Vector2 direction = (movetoDest - transform.position).normalized;
transform.GetComponent<Rigidbody>().velocity = direction * speed;

if (Vector2.Distance(transform.position, movetoDest) < stopDistanceOffset)
movetoDest = Vector2.zero;
}
else
transform.GetComponent<Rigidbody>().velocity = Vector2.zero;
}
}
``````

and here:

``````private void UpdateMove()
{
if (movetoDest != Vector3.zero && transform.position != movetoDest)
{
Vector3 direction = (movetoDest - transform.position).normalized;
transform.GetComponent<Rigidbody>().velocity = direction * speed;

if (Vector3.Distance(transform.position, movetoDest) < stopDistanceOffset)
movetoDest = Vector3.zero;
}
else
transform.GetComponent<Rigidbody>().velocity = Vector3.zero;
}
}
``````

Keep in mind some of this code may appear different due to my adaption of it

Please let me know if this is too vague and how I can help you help me! this is my first post so I’m not to sure on some of the etiquette around here

You need to set:
direction.z = 0;
after line 5 in your second code snippet to work in the x-y plane.

In the first snippet, 2D and 3D vectors are being mixed. I’m guessing you are only seeing the “else” statement being hit in the debugger?