Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by xToxicInferno · Aug 15, 2010 at 05:26 PM · movementrotatemovegrid

Help with Grid Movement

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);
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

Answer by jashan · Aug 18, 2010 at 05:15 AM

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:

  • Highlighting tiles on a hexagonal grid
  • Infinite 2D placement grid
  • Grids / Array / Tower Defence
  • Need help optimizing grid initialization.

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 ;-) ).

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image · Aug 18, 2010 at 05:28 AM 0
Share

I was partway through typing up something similar (but not as detailed), so I'll 'second' this ins$$anonymous$$d.

avatar image jashan · Aug 18, 2010 at 05:44 AM 0
Share

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 ;-) ).

avatar image xToxicInferno · Aug 18, 2010 at 11:37 PM 0
Share

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.

avatar image jashan · Aug 19, 2010 at 10:54 AM 0
Share

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 ;-) ).

avatar image jashan · Aug 19, 2010 at 10:57 AM 0
Share

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

Show more comments
avatar image
2

Answer by Eric5h5 · Aug 18, 2010 at 03:06 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How do I make a 3D character move and rotate (turn) the same direction they move? c# 0 Answers

How to Move an Object down until a certain point then activate a function 1 Answer

Trouble with correct move and rotation of Rigibody 0 Answers

Rotating Player 0 Answers

movement doesnt stop when jump pad is tapped at the same time 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges