Hi, I’d like to know how to move objects based on 2D Array map. I’m not familiar with 2D Array, but it seems really basic and fast performance to make a game, so I want to study about!
I’ve been googling and watching Youtube tutorials, but I haven’t find really helpful info. I’ve managed to randomly instantiate prefabs based on 2D array so far, but I don’t know how to move the player based on the map.
What I want to do is this.
The player to move one grid (prefab) side way(A/B in the pic!) when I get left/right key up.
Slide all prefab to down/up and get new prefab at the top/bottom when I get up/down key up. so that, it looks like the player moving forward/back forward like a treadmill
Should I move the player inside of the array? Let’s say player to be int =5 in array map?
Or should I move the player using transform.translate then checked if the position of player is walkable int=0 in the array map?
How can I know where the player position in the array, or where the A/B are?
I’d really appreciate if you could give me any help
Well a good way to control the players position on his 2D grid is to add or remove one from the index and then set the position of the player according to the grid. I assume you want a snapping effect, so you would use set the Transform position of the player directly, without interpolation. Here my concept.
static class Map : MonoBehaviour {
private int[,] map;
public int width, height;
public Map(int width, int height) {
this.width = width;
this.height = height;
map = new int[this.width,this.height];
}
public SetPoint(int index, int x, int y) {
map[x,y] = index;
}
public void Render() {
for(int x=0;x<width;x++){
for(int y=0;y<height;y++) {
switch(map[x,y]) {
case 0:
// Render air or grass
GameObject grass = GameObject.CreatePrimitive(PrimitiveType.Cube);
grass.transform.position = new Vector3(x,y,0);
grass.GetComponent<Renderer>().material.color = Color.green);
case 1:
// Render player
GameObject player = GameObject.CreatePrimitive(PrimitiveType.Cube);
player.transform.position = new Vector3(x,y,0);
player.GetComponent<Renderer>().material.color = Color.red);
case 2:
// Render object
GameObject wall = GameObject.CreatePrimitive(PrimitiveType.Cube);
wall.transform.position = new Vector3(x,y,0);
wall.GetComponent<Renderer>().material.color = Color.blue);
}
}
}
}
}
static class Player : MonoBehavior {
private int x = 0;
private int y = 0;
public Player(int x, int y, Map m) {
this.x = x;
this.y = y;
m.SetPoint(1, this.x, this.y);
}
public void Move(int x, int y) {
m.SetPoint(0, this.x, this.y);
this.x += x;
this.y += y;
m.SetPoint(1, this.x, this.y);
}
}
Sorry for the late reply, my Email had been buggy for while so I din’t notice that I got reply
I want to move the player only left or right, not forward nor backward.
For the forward and backward movement, I want to player to be still & move all the tiles.
Thank you, Thank you Thank you!!!
I thought I was doomed !!
I didn’t notice this helpful reply until now due to that my Email notification has been crashed.
I will post the result after I get home and try the code
I noticed too late that you wanted all the tiles to move forward instead of the player. But I think I have enough framework for an efficient drawing system that supports multiple ground types. Hopefully you can elaborate this. I’d like to see how your project progresses. Contact me through Skype: myisaak123
(Q1)
What are they?
Are they methods ? Should I not write ‘void’ or ‘variable names’ ?
They looks like some kind of variable type…but I could be wrong.
Could you tell me the name of them ? reflection?
(Q2)
From 39 is another class, right?
So I made a new class (script) and wrote down your code, but I can’t call the ‘SetPoint’ from the Map class.
Also, the Map class has an error that say “Class, struct, or interface method must have a return type”.
I’m not copy and paste your whole code because I have already create a map that I want, instead of copy & paste, I add some codes that I might need from your’s.
e.g. “Player class” , “SetPoint in the Map class”…
And I change both class to be ‘static’ as you wrote.
(Q3)
Do I not need an Update function ?
I really wish to, but my wifi is as slow as a snail, you might not be able to hear what I say :((
Thank you for your generous offer !