Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

2D Movement Controls

Discussion in 'Getting Started' started by OpLex, Jan 31, 2017.

  1. OpLex

    OpLex

    Joined:
    Sep 3, 2012
    Posts:
    8
    Hello,

    I'm currently working on a 2D game that requires the character to move to specific coordinates left, right, up and down. I'm programming the game in C# and require some help on where to start. I was told in the forums that I might be looking to use a "path-finder". Or something along the lines of that.

    Here is a poorly drawn image of what I'm looking for.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,835
    Please tell us more about your game. Is it grid-based? Are there obstacles? Do you always move just one step (i.e. to an immediate neighbor), or do you want the player to somehow indicate "go way over here on the other side of the playing field" and make the unit find a way to get there?

    If there is some famous game that works similar to what you want to make, perhaps you can point that out for reference.
     
  3. OpLex

    OpLex

    Joined:
    Sep 3, 2012
    Posts:
    8
    The game is similar to the classic Game and Watch games preferably the title "Chef" The player moves the character left and right obtaining collectibles and each time the character moves the frame animation changes.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,835
    OK... I don't know that game, but some googling turned up this.

    You didn't answer my other questions, which is going to make it hard to give you much help.

    Try this: just throw a sprite into your scene, and attach a simple script that checks for Input.GetKeyDown(KeyCode.LeftArrow), and if that's true, moves the sprite one unit to the left (i.e. transform.position += new Vector3(-1, 0, 0) ). See how that suits you. Then add similar code for right, up, and down.
     
  5. OpLex

    OpLex

    Joined:
    Sep 3, 2012
    Posts:
    8
    Thank you again for being very quick and patient with these questions. I'm using GetKeyDown but didn't try the (i.e. transform.position += new Vector3(-1, 0, 0) ), so I will try that when I get the chance. To answer your other questions as simply as possible...

    The game is not grid-based (not that I know of at least.) There are no obstacles other than the boundaries the character is blocked by. You always move one step at a time, only one frame animates or changes after the character moves to the location. You always move one step at a time using the Keyboard left, right, up or down arrow keys.

    You can see a better example of the animation frame changes here:
    )
     
    JoeStrout likes this.
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,835
    Well, the animation is completely separate from the movement. I would advise you to tackle just one at a time.

    Let us know how that transform.position change works out for you. (And if you want to ask about the code, please post it using code tags so it's easily readable.)
     
    OpLex likes this.
  7. OpLex

    OpLex

    Joined:
    Sep 3, 2012
    Posts:
    8
    Joe,

    Thank you so much for you input! I worked the code you gave me with the code I made on my character and it worked!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.     public class Player1 : MonoBehaviour {
    5.  
    6.     void Start () {
    7.    
    8.     }
    9.  
    10.     void Update () {
    11.    
    12.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    13.         {
    14.             transform.position += new Vector3 (-1, 0, 0);
    15.         }
    16.         if (Input.GetKeyDown(KeyCode.RightArrow))
    17.         {
    18.             transform.position += new Vector3 (1, 0, 0);
    19.         }
    20.            
    21.     }
    22. }
    23.  
    On to the next ;)
     
    JoeStrout likes this.