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

Grid Based Game Movement (Megaman Style)

Discussion in 'Scripting' started by EisteePrinz, Jan 26, 2021.

  1. EisteePrinz

    EisteePrinz

    Joined:
    Jan 11, 2021
    Posts:
    2
    Hey guys!

    First of all if this is in the wrong forum category please feel free to move it!

    Im currently working on a small game for a university project and im stuck with the basic movement of the game. The games inspiration comes from Megaman Battle Network and One Step From Eden. The character moves from field to field but always looks in the same direction (towards the enemy field).

    I have 2 ideas which im currently trying to code:
    1) Im trying to use rays to determine the border of the map. This is working so far, i can move my Player and as soon as a ray detects a cube with the tag "Wall" it blocks the movement in that direction. But for whatever reason whenever i press a button in any direction to often multiple times the Sphere (Player) ignores the Wall and passes right through it. I will upload a small video so you can see what i mean :)



    You can see how at the beginning the Players stops as soon as i reach a wall. But then when i press to quickly he ignores the wall.

    The movement script is uploaded aswell.

    2) My next idea was to put the nodes on the left side, which are only for the player, into a 2D Array. For example bottom left corner is 0/0 and when i go one node to the right it is 0/1 and so on. So whenever the current position of my player would for example go further than 0/2 which is the bottom right player node it shoudlnt let him move anymore into that direction.
    This is my approach and the beginning of the 2D Array, but it isnt working. Im trying to assign the playerNodes to the 2D Array. (Node 1 = 0,0), (Node 2= 0,1) (Node 3 = 0,2).... and so on.

    Code (CSharp):
    1.     public static Transform[] playerNodes;                  
    2.     public static Transform[,] coordinates;
    3.  
    4.     private void Awake()
    5.     {
    6.         playerNodes = new Transform[transform.childCount];
    7.         // coordinates = new Transform[]
    8.  
    9.  
    10.         for (int i = 0; i < playerNodes.Length; i++)
    11.         {
    12.             for (int j = 0; j < playerNodes.Length; j++)
    13.             {
    14.                 coordinates[i,j] = playerNodes.position;
    15.             }
    16.         }
    17.  
    18.  
    19.     }

    Edit: coordinates[i,j] = playerNodes.position; in the for loop isnt working. I get the error CS0029."Cannot implicity convert type 'UnityEngineVector3' to 'UnityEngine.Transform'

    I really hope you can help me :confused:
     

    Attached Files:

    Last edited: Jan 26, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Option #2 is definitely the way to go: keep your own notion of a 2D array where each "thing" is in the game. Use this for all gameplay logic, and then have a separate system that "presents" each piece in the game, animates, etc. Animation obviously has no impact on game logic, so you want those systems completely divorced from each other.

    Nobody memorizes error numbers... nobody!

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand errors in general:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  3. ensiferum888

    ensiferum888

    Joined:
    May 11, 2013
    Posts:
    317
    Line 14 in your Awake function, you're assigning a Vector3 to an array of Transform, either change the array type to Vector3 or assign the transform:

    Either:
    Code (CSharp):
    1. public static Vector3[,] coordinates;
    Or:
    Code (CSharp):
    1. for (int i = 0; i < playerNodes.Length; i++)
    2.         {
    3.             for (int j = 0; j < playerNodes.Length; j++)
    4.             {
    5.                 coordinates[i,j] = playerNodes[i+j];
    6.             }
    7.         }
    For the movement issue I'm not quite sure what's happening, can you confirm that pressing the button quickly still outputs Wall in the console?
     
  4. EisteePrinz

    EisteePrinz

    Joined:
    Jan 11, 2021
    Posts:
    2
    Thanks alot for your answer. Sorry for not replying the whole week, had technical issues at home.
    I get a NullReference Error after i changed the array type to Vector3. The array doesnt have a value assigned at the beginning, but im not quiet sure which value is correct to assign, since the field of the player should be variable in the future. At the moment its a 3x6 field as you can see but this wont be the case for every level, at least thats the plan.

    For the movement issue after inserting a Debug.Log i checked in the console whats happening. If i move slow he always puts out "Wall" whenever im close to one, which is the correct behavior. But whenever i press to quickly he just puts out "Wall" once and then passes right through it.

    Code (CSharp):
    1.     bool Valid()
    2.     {
    3.                 Ray myRay = new Ray(transform.position + new Vector3(0, 0.25f, 0), transform.forward);
    4.            
    5.                 RaycastHit hit;
    6.                 Debug.DrawRay(myRay.origin, myRay.direction, Color.red);
    7.                 if (Physics.Raycast(myRay, out hit, rayLength))
    8.                 {
    9.                     if (hit.collider.tag == "Wall")
    10.                     {
    11.                         Debug.Log("Wall");
    12.                         return false;
    13.                     }
    14.                 }
    15.             return true;
    16.     }