Grid based movement, sprite change to simulate movement
Hello,
I am in the process of creating a first person dungeon crawler and I have run into an issue. I am pretty new to the concept of coding so I am sure there is a much easier way to do what I have done but I wasn't sure what the correct coding process was.
Anyway, I am creating a first person dungeon crawler that looks like this:
My idea is in a 9 square grid with each grid square having 4 pictures (North, East, South, West.)
Here is the code I use to change the pictures to depict the feeling of walking:
public Sprite One_North;
public Sprite One_East;
public Sprite One_South;
public Sprite One_West;
public Sprite Two_North;
public Sprite Two_East;
public Sprite Two_South;
public Sprite Two_West;
public Image gameplayImageTransition;
private DirectionDetection directionDetection;
public string currentTileNumber;
public string currentDirection;
public string currentTileAndDirection;
// Use this for initialization
void Start () {
currentDirection = "North";
currentTileNumber = "Two";
gameplayImageTransition.sprite = Two_North;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.UpArrow)){
Debug.Log ("Up Arrow");
UpdateWalkPathForward ();
}
if(Input.GetKeyDown(KeyCode.LeftArrow)){
Debug.Log ("Left Arrow");
UpdateWalkPathLeft ();
}
}
void UpdateWalkPathForward(){
if (currentDirection == "West") {
ConvertCurrentTileNumberToInt ();
int curTileDummy = System.Convert.ToInt32 (currentTileNumber) - 1;
currentTileNumber = System.Convert.ToString (curTileDummy);
ConvertCurrentTileNumberToString ();
gameplayImageTransition.sprite = (Sprite)this.GetType ().GetField (currentTileNumber + "_West").GetValue (this);
} else if (currentDirection == "East") {
ConvertCurrentTileNumberToInt ();
int curTileDummy = System.Convert.ToInt32 (currentTileNumber) + 1;
currentTileNumber = System.Convert.ToString (curTileDummy);
ConvertCurrentTileNumberToString ();
gameplayImageTransition.sprite = (Sprite)this.GetType ().GetField (currentTileNumber + "_East").GetValue (this);
}
}
void UpdateWalkPathLeft(){
if(currentDirection == "North"){
currentDirection = "West";
gameplayImageTransition.sprite = (Sprite)this.GetType().GetField(currentTileNumber + "_West").GetValue(this);
}else if(currentDirection == "West"){
currentDirection = "South";
gameplayImageTransition.sprite = (Sprite)this.GetType().GetField(currentTileNumber + "_South").GetValue(this);
}else if(currentDirection == "South"){
currentDirection = "East";
gameplayImageTransition.sprite = (Sprite)this.GetType().GetField(currentTileNumber + "_East").GetValue(this);
}else if(currentDirection == "East"){
currentDirection = "North";
gameplayImageTransition.sprite = (Sprite)this.GetType().GetField(currentTileNumber + "_North").GetValue(this);
}
}
public void ConvertCurrentTileNumberToInt(){
if (currentTileNumber == "One") {
currentTileNumber = "1";
}else if (currentTileNumber == "Two") {
currentTileNumber = "2";
}
}
public void ConvertCurrentTileNumberToString(){
if (currentTileNumber == "1") {
currentTileNumber = "One";
}else if (currentTileNumber == "2") {
currentTileNumber = "Two";
}
}
}
So basically you start at Two_North (tile two, facing North) and in my example you press the left arrow which changes the pictures from Two_North to Two_West. Then when you press the Up arrow it will switch the picture from Two_West to One_West like you were walking one square up.
One of the main things I have run into is getting the math to work. So far it works okay but my issue is that if I am on tile four and I'm facing west and press up on the keyboard I will jump from Four_West to Three_West which should not happen. Instead of jumping to the top of the grid the player should just stand in Four_west and not move anywhere (basically like running into a wall.)
If anyone has any tips for me that would be amazing, at this point I am stuck trying to figure out what to do with this. Thank You for your help.
Your answer
Follow this Question
Related Questions
creating a 2d peeling system in unity 0 Answers
When all of the enemies in a room dies the doors dont open. 0 Answers
Help with Instantiation and Sprite changing lag 0 Answers
Is it possible to create a texture that hides a specific texture? 0 Answers
Creating rooms and corridors by Random dungeon generation 0 Answers