Search Unity

Moving a character exactly 1 unit before checking next movement

Discussion in 'Scripting' started by thestrandedmoose, Jan 19, 2019.

  1. thestrandedmoose

    thestrandedmoose

    Joined:
    Jul 21, 2015
    Posts:
    70
    Hey all-
    I'm trying to create a 3D game where a player can only move along the exact coordinates of the Unity grid.
    I also want the player to continue moving the same direction until there is an opening in the desired direction.

    For instance- if I swipe left and the player moves left, and then I swipe up, he will not move up until A: He reaches an open intersection, and B: He's at a whole number coordinate (for instance -10, 0 , 0,)

    So far, I've set up a raycast system that can check for walls, but I can't figure out how to move the player exactly one unit before checking if the coast is clear

    Does anyone know how I could achieve something like this?
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @thestrandedmoose

    Well you could go for Pacman tutorials. Not unity or C# specific.

    Your explanation sounds just like it.

    If you mean by moving along coordinates, that you want to move along main 3d world axis?

    You basically have to move from node to node.

    When not near/exactly at node, allow only flip on axis you are moving along (if you want, probably not based on your description).

    When you reach intersection node, you can allow your character to choose any direction.

    Then your movement ends up being just repeated moves from node to node.

    Round the coordinates of next node and current position when arriving at intersection.

    Then you can move between nodes with MoveTowards something like this...

    Code (CSharp):
    1.  
    2. transform.position = Vector3.MoveTowards(transform.position, posNext, yourSpeed * Time.deltaTime);
    3.  
    This is one way to do grid movement. Not sure if it gives you the best results but I've used similar code myself.
     
    thestrandedmoose likes this.
  3. thestrandedmoose

    thestrandedmoose

    Joined:
    Jul 21, 2015
    Posts:
    70
    Thanks for your response,
    I've looked at Pacman tutorials, but there aren't any that cover 3D movement unfortunately, and it's not as simple as changing all the Vector2's to Vector 3s... The Node system might work.. I'd have to think about how to implement that.

    I kind of wanted to just use Unity's grid and colliders as my logic for where to turn though... that way I wouldn't have to set up a bunch of nodes, but maybe that is my best option at this point. Thanks again!
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    well I meant nodes in logical sense. Just points in space when you decide nextspot to move ro when you enter an intersection. Vectors.

    You only would need positions, I don’t see need for colliders, if you really want to move along main axis.

    What do you mean by 3d not same as 2d?

    I understood that you are going to move along ground plane... if so, it is exactly the same as Pacman.

    But if you need moment along y, then instead of x and z you need to handle y-axis movement.
     
  5. thestrandedmoose

    thestrandedmoose

    Joined:
    Jul 21, 2015
    Posts:
    70
    I simply mean I was able to find 2D unity projects that work for Pacman, but I was unable to find any 3D ones that performed well and were simple. In addition, I couldn't find any tutorials on grid movement that were accurate, UNTIL I stumbled across this:

    For anyone else looking to achieve really good, simple grid movement- there is a great video series on it here:


    I was able to modify his code to run like pacman. I could have also done the nodes process you mentioned and not had colliders at all, but I found it to be really easy way to build levels with grid snapping. I simply made a grid movement script like the one above, but provided a continunous direction. Then if my player detects a wall in the space ahead, he will stop.

    This also works nicely if I want to have doors that open or close or have the map shift around during gameplay :)