# Help with Grid Movement

 0 I am trying to make a game controls similar to a Turn Based Strategy game where the player moves on a grid. I am planing on having the player be a sprite so it i will be 2D. The way i plan to implement it is simple. Have a Plane with a 'GridDetection' game object right below the center of the plane. This way i can have a empty game object connected to the Player to shoot out rays to see if that is a legitimate place to move. As in, i do not want the player to be able to move out side of the grid or into spaces within the grid they aren't aloud to. I plan to make it so when the GridDetection object is hit, it sends information to the player about what type of terrain it is telling the player if it can move on it or has to wait (like Water, if the player is a boat it can, but not as a person). The problem is, i can't seem to figure out how to make it move correctly. I want to shoot out a ray into the direction indicated by the players input (WASD) and check if that is a valid space to move. If so, rotate to face it, then move forward 10 units (that is the distance between the centers of the the grids). So the question is: How do i rotate to face the correct direction then move to it. Thanks for you help. Here is the script i made that does not work and is not complete as i work on one part, test it, then code the next part, test it....as it didn't work at first i havn't finished it. ``````var RayLength : int = 10; var rotateSpeed : int = 10; var moveSpeed : int = 10; var Parent : GameObject; private var leftRotate : boolean = false; private var rightRotate : boolean = false; private var turnAround : boolean = false; private var move : boolean = false; function FixedUpdate () { var left = (transform.TransformDirection(-Vector3.right)); var right = (transform.TransformDirection(Vector3.right)); var forwards = (transform.TransformDirection(Vector3.forward)); var backwards = (transform.TransformDirection(-Vector3.forward)); var hit : RaycastHit; if(Input.GetKeyDown("a")) { if(Physics.Raycast(transform.position, left , hit,RayLength)){ if(hit.transform.tag == "GridDetection") { Debug.Log("Left"); leftRotate = true; } } } if(Input.GetKeyDown("d")) { if(Physics.Raycast(transform.position,right,hit,RayLength)){ if(hit.transform.tag == "GridDetection") { } } } if(Input.GetKeyDown("s")) { if(Physics.Raycast(transform.position,backwards,hit,RayLength)){ if(hit.transform.tag == "GridDetection") { } } } if(Input.GetKeyDown("w")) { if(Physics.Raycast(transform.position,forwards,hit,RayLength)){ if(hit.transform.tag == "GridDetection") { } } } if(leftRotate) { Parent.transform.rotation.y -= 1 * Time.deltaTime; if(Parent.transform.rotation.y == 270) { leftRotate = false; move = true; } } if(move) { Parent.transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime); } } `````` more ▼ asked Aug 15 '10 at 05:26 PM xToxicInferno 485 ● 24 ● 28 ● 41 Peter G 15.1k ● 16 ● 44 ● 137 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 The nice thing about using a grid is that it simplifies things a lot. You can create a simple class or struct that represents the different attributes of your grid (like "is water" or "is forbidden", whatever you need). Also, in a grid you only have 4 possibilities of movement (or 8 if you decide to allow diagonal movement). So, depending on the attributes of your game space you can either create an array of your "Tile" class/struct, or create a linked list kind of structure. You will use a linked list in cases where the game space is more sparse because an array would be a bit of waste in that case. However, an array may still make sense because it allows random access (which a linked list does not). So, if you need to be able to check "what's going on in position x,y", an array might be nice - if you just need to know "what's coming when I move north", a linked list is fine. Even with the linked list, you could create lookup structures (Dictionary) for random access - but keep in mind that this will only make sense if you have a lot of "dead space" where the player can't move and nothing ever happens. Assuming you're using a linked list structure like this (this code is just meant as illustration, so don't worry too much about the syntax which is C# which I find much more readable): EDIT: Thanks to Quietus for pointing out that structs might not work for this due to their "by-value-nature" ... I'm not sure if anyone actually tried this but it probably wouldn't have worked in the original form (public struct GridTile). With the new form (public class GridTile) it should work fine. ``````public class GridTile { public GridTile north; // JavaScript would be var north : GridTile; public GridTile south; public GridTile west; public GridTile east; public bool isWater = false; // just a default value public bool isAccessible = true; // just a default value } `````` And your player having an attribute "GridTile myCurrentTile;", you could do this: ``````if (notMoving && Input.GetKeyDown("w")) { if (myCurrentTile.north.isAccessible) { if (!myCurrentTile.north.isWater || hasBoat) { StartMovingNorth(); } } } `````` So, "notMoving" would be a boolean that you'd use to check if the player is currently in a moving animation, and "hasBoat" is a bool which indicates whether the player currently has a boat that he could use to move over water. Obviously, depending on your game design there's a lot of possibilities (like, if the player doesn't have a boat but if the player object is a boat, you'd have to check for "water and boat" or "not water and not boat"), but this should give you a starting point. One tricky thing with those grids: You'll need to spend some time thinking about how to author the grid attributes. Contrary to what I said in the beginning, you might actually implement those "Tiles" as MonoBehaviours and lay them out in the editor. And you could then easily write editor scripts to design your levels ... but that would be another topic. In the end, these things really depend a lot on how you want to set up things so I'm trying to just give a general idea of what are possible approaches. EDIT: There's some related questions / answers that might help setting up the grid system: And one more thing came to my mind regarding whether to use an array or linked list structure: Arrays work best with rectangular maps while the linked list approach allows any kind of shape (and it could even support "teleporters" trivially by simply assigning a tile that is on a completely different location to one of the slots ... hm ... sounds like creating grid-based games could be quite a bit of fun ;-) ). more ▼ answered Aug 18 '10 at 05:15 AM jashan 10.3k ● 25 ● 43 ● 117 I was partway through typing up something similar (but not as detailed), so I'll 'second' this instead. Aug 18 '10 at 05:28 AM Marowi Thanks ... yeah, that happens to me all the time, too. But I think it shows that Unity Answers really works which I think is really nice. Sometimes it's really surprising how quickly questions get answered here (I hadn't noticed this one earlier, though - but the bounty surely helps with motivation ;-) ). Aug 18 '10 at 05:44 AM jashan Alright this seems very good, but how do i go about checking what is 'North', 'South, etc... I have a few months of Unity experience but all my scripting knowledge is self taught. So in short, i do not know the what a linked list is, or a structure (sad isn't it) it is just something i do in my free time, and i hope to get better with but am still learning, hence why i offered a bounty for such a simple question, i'm doing it just to learn. Thanks again for the answer. Aug 18 '10 at 11:37 PM xToxicInferno This should help: http://en.wikipedia.org/wiki/Linked_list ... basically, what I'm suggesting here is a "4-way-linked list", that's what the public properties in GridTile are, so for navigation, you could write myCurrentTile.north.north.north.west.west (to see what's ahead 3 steps north and 2 steps west). You could also say myCurrentTile.north.south ... which would reference your own tile (just for the fun of it ;-) ). So, obviously you'll have to make sure that the tiles are connected in the right way (ironically, you might be using Raycasts/Linecasts in your editor scripts for that ;-) ). Aug 19 '10 at 10:54 AM jashan PS: ... just as a sidenote, those linked lists can also be very helpful to implement pathfinding algorithms like A*. For an intro on that, see: http://www.policyalmanac.org/games/aStarTutorial.htm Aug 19 '10 at 10:57 AM jashan add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 I wouldn't use raycasting or anything. You can use something like this for smooth grid movement, and use a 2D array to track what sort of square the player is in, to determine whether an attempted move is valid or not. more ▼ answered Aug 18 '10 at 03:06 AM Eric5h5 81.5k ● 42 ● 133 ● 529 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x1426
x747
x516
x276

asked: Aug 15 '10 at 05:26 PM

Seen: 5254 times

Last Updated: Aug 15 '10 at 06:07 PM