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.
  2. Dismiss Notice

Need help with grid based movement in C#

Discussion in 'Scripting' started by CalvBore, Oct 28, 2014.

  1. CalvBore

    CalvBore

    Joined:
    Oct 23, 2014
    Posts:
    9
    Hey guys, I'm fairly new to Unity. I've been playing around with the 2D editor and scripting with C#. I've run into a bit of a snag with my current project. I'm trying to create a top-down movement style similar to Pokemon or Final Fantasy.

    Here's what I have so far:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerScript : MonoBehaviour
    5. {
    6.     public float speed = 2.5f;
    7.     bool canMove;
    8.     Vector3 pos;
    9.     Transform tr;
    10.  
    11.     void Start ()
    12.     {
    13.         bool canMove = true;
    14.  
    15.     }
    16.  
    17.     void Movement ()
    18.     {
    19.         if (Input.GetKey (KeyCode.W))
    20.         {
    21.             transform.Translate(Vector3.up * speed * Time.deltaTime);
    22.         }
    23.         if (Input.GetKey (KeyCode.A))
    24.         {
    25.             transform.Translate(Vector3.left * speed * Time.deltaTime);
    26.         }
    27.         if (Input.GetKey (KeyCode.S))
    28.         {
    29.             transform.Translate(Vector3.down * speed * Time.deltaTime);
    30.         }
    31.         if (Input.GetKey (KeyCode.D))
    32.         {
    33.             transform.Translate(Vector3.right * speed * Time.deltaTime);
    34.         }
    35.  
    36.     }
    37.  
    38. void FixedUpdate ()
    39.     {
    40.         if (canMove = true)
    41.         {
    42.             Movement ();
    43.  
    44.         }
    45.  
    46.     }
    47. }
    I should also mention that I found a JavaScript code that worked exactly how i wanted it to somewhere amongst the interwebs, but I'm completely illiterate with JavaScript so I've been trying to recreate it in C#.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,487
    Line 13... Remove the bool. That bool makes a local variable, masking your instance variable, which stays false.

    Also line 40, use == not = for comparisons.

    And do pay attention to your compiler warnings. ;)

    Kurt
     
  3. CalvBore

    CalvBore

    Joined:
    Oct 23, 2014
    Posts:
    9
    Thanks, I don't know how I missed that on line 40 haha.
     
  4. CalvBore

    CalvBore

    Joined:
    Oct 23, 2014
    Posts:
    9
    Also, here is the JavaScript I found earlier that does the sort of movement I am going for.


    Code (JavaScript):
    1. #pragma strict
    2. private var speed = 2.0;
    3. private var pos : Vector3;
    4. private var tr : Transform;
    5. function Start() {
    6.      pos = transform.position;
    7.      tr = transform;
    8. }
    9. function Update()
    10. {
    11.      if (Input.GetKey(KeyCode.D) && tr.position == pos)
    12.      {
    13.          pos += Vector3.right;
    14.      }
    15.      else if (Input.GetKey(KeyCode.A) && tr.position == pos)
    16.      {
    17.          pos += Vector3.left;
    18.      }
    19.      else if (Input.GetKey(KeyCode.W) && tr.position == pos)
    20.      {
    21.          pos += Vector3.up;
    22.      }
    23.      else if (Input.GetKey(KeyCode.S) && tr.position == pos)
    24.      {
    25.          pos += Vector3.down;
    26.      }
    27.    
    28.      transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
    29. }  
     
  5. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Here's the C# version of that JS:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerScript : MonoBehaviour
    6. {
    7.     private float speed = 2.0f;
    8.     private Vector3 pos;
    9.     private Transform tr;
    10.  
    11.     private void Start()
    12.     {
    13.         pos = transform.position;
    14.         tr = transform;
    15.     }
    16.  
    17.     private void Update()
    18.     {
    19.         if (Input.GetKey(KeyCode.D) && tr.position == pos)
    20.         {
    21.             pos += Vector3.right;
    22.         }
    23.         else if (Input.GetKey(KeyCode.A) && tr.position == pos)
    24.         {
    25.             pos += Vector3.left;
    26.         }
    27.         else if (Input.GetKey(KeyCode.W) && tr.position == pos)
    28.         {
    29.             pos += Vector3.up;
    30.         }
    31.         else if (Input.GetKey(KeyCode.S) && tr.position == pos)
    32.         {
    33.             pos += Vector3.down;
    34.         }
    35.  
    36.         transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime*speed);
    37.     }
    38. }
    39.  
    It's almost identical, you just need to change how the variables are declared.

    The difference between your version and this version is that your version doesn't "stick" to the grid, it just lets you move freely, even diagonally, and stop in between two grid squares. If you want to have the character stick to the grid and not move diagonally you have to do it as above.
     
    CalvBore likes this.
  6. CalvBore

    CalvBore

    Joined:
    Oct 23, 2014
    Posts:
    9
    Thank you. This helps me out immensely.