- Home /
How to create a 2D grid based array with passable/impassable grids for movement ?
How can I create a 2D array that allows movement through only the lanes, but not the dark grey blocks as in the picture below ? I tried using rigidbody and colliders and the result is plain terrible, as the sphere resembles a woodpecker drilling into a branch everytime there is a collision.
Thanks for taking a look into this.
The code that I'm using to move in a grid like fashion is given below:
using UnityEngine;
using System.Collections;
public class ControllerScript : MonoBehaviour {
public float speed = 1.0f;
private Vector3 endpos;
private bool moving = false;
void Start () {
endpos = transform.position;
}
void Update () {
if (moving && (transform.position == endpos))
moving = false;
if(!moving && Input.GetKey(KeyCode.W)){
moving = true;
endpos = transform.position + Vector3.up;
}
if(!moving && Input.GetKey(KeyCode.S)){
moving = true;
endpos = transform.position + Vector3.down;
}
if(!moving && Input.GetKey(KeyCode.A)){
moving = true;
endpos = transform.position + Vector3.left;
}
if(!moving && Input.GetKey(KeyCode.D)){
moving = true;
endpos = transform.position + Vector3.right;
}
transform.position = Vector3.MoveTowards(transform.position, endpos, Time.deltaTime * speed);
}
}
Assu$$anonymous$$g you built your grid so all the lanes are on unit boundaries, and the sphere starts in a lane, there should not be any collisions.
as robertbu said. you may have a scaling issue, however if you did want to go down the array route its something like
pseudocode:
//where x is an illegal square
mapArray[5][5] = o,x,o,x,o,
o,x,o,x,o,
o,x,o,x,o,
o,x,o,x,o,
o,x,o,x,o
Vector2 playerPos (0,0)
moveFunction()
{
Vector2 oldPos = playerPos;
if{inputUp}
{
playerPos += Vector2(0,1);
}
if left, if right, if down etc....
//check player position in array
if(mapArray[playerPos.x][playerPos.y]!='x')
{
//do transformy stuff
}
else
{
playerPos = oldPos;
}
}
or something :D
@robertbu + @bubzy - All the tiles are 1 unit of 100 pixels each, so they are aligned to a grid pretty much. Building the level is the only thing I can do so far and would require some more guidance to make it work. How do I make the 2D array correspond to the level ? How do I connect the array to the level and tell the sphere object to not move even if I'm pressing the button to make it go into the direction of a wallblock ? Although @bubzy provided a rough code, I'm having a hard time understanding it. A deeper explanation as to how this works or links to resources that better explain this would be very helpful. Thanks for taking the time to reply.
The below is the kind of array that I wish to create, with 1 being unwalkable.
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Did you take the Rigidbody off the ball? Given your code, without a Rigidbody, I don't see how the ball is colliding with a tile. Your code only allows one movement (horizontal or vertical), and it locks out other movement until the move is complete. As long as you clamp the ball so that it cannot go outside, I don't see a way for a collision to occur.
Yes, I did take the rigidbody off the ball since this type of grid avoidance doesn't require physics. It is true that until the movement is complete, other movement is locked out. When I'm adjacently onto the left of the wall block and when I press D, the sphere moves on top of the wall block tile. This is exactly what I wish to fix. How do I clamp the movement of the back so that it never enters a wall block tile?
Answer by coolbird22 · Sep 18, 2014 at 04:55 PM
@bubzy replied to the question with an example. Going through it all made a lot of sense and helped a lot. Here is his reply.
I don't usually do this but http://westwalescse.net/unity/arrayDemo.rar take a look, theres a few annoying things with positioning and whatever, but the idea is there.
it really does look as though you could do with brushing up on your coding a little bit though, this is bread-and-butter logic :)
Your answer
Follow this Question
Related Questions
Keeping track of array objects 2 Answers
Moving cubes into a grid formation 1 Answer
Ignore Colliders of an Object 1 Answer
Moving a character into a 2d int array 0 Answers
Visualize array in game 2 Answers