Search Unity

Top Down 2D movement script adaption

Discussion in '2D' started by Jonnoiscooler, Apr 5, 2015.

  1. Jonnoiscooler

    Jonnoiscooler

    Joined:
    Apr 2, 2015
    Posts:
    2
    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 (CSharp):
    1. private void UpdateMove()
    2.     {
    3.         if (movetoDest != Vector3.zero && transform.position != movetoDest)
    4.         {
    5.             Vector2 direction = (movetoDest - transform.position).normalized;
    6.             transform.GetComponent<Rigidbody>().velocity = direction * speed;
    7.  
    8.             if (Vector2.Distance(transform.position, movetoDest) < stopDistanceOffset)
    9.                 movetoDest = Vector2.zero;
    10.         }
    11.         else
    12.             transform.GetComponent<Rigidbody>().velocity = Vector2.zero;
    13.     }
    14. }
    15.  
    16.  
    17.  
    18.  
    19.  
    and here:


    Code (CSharp):
    1. private void UpdateMove()
    2.     {
    3.         if (movetoDest != Vector3.zero && transform.position != movetoDest)
    4.         {
    5.             Vector3 direction = (movetoDest - transform.position).normalized;
    6.             transform.GetComponent<Rigidbody>().velocity = direction * speed;
    7.  
    8.             if (Vector3.Distance(transform.position, movetoDest) < stopDistanceOffset)
    9.                 movetoDest = Vector3.zero;
    10.         }
    11.         else
    12.             transform.GetComponent<Rigidbody>().velocity = Vector3.zero;
    13.     }
    14. }

    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 :p
     
  2. Catfang007

    Catfang007

    Joined:
    May 8, 2014
    Posts:
    5
    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?