Adding character facing directions
The last task we have is to add a functionality that will allow the player to change the direction he or she faces. Then, we can add some functionality that will allow the sword to swing in the same direction that the player is facing. To achieve this, we will have to start in the MovingObject
class.
The AttempMove
function is where the direction of the player is managed. This is where we can poll what direction the player is moving in, and change the sprite direction accordingly. Code snip 6.9 shows the update that should be added to the top of the AttemptMove
function definition:
1 protected virtual bool AttemptMove <T> (int xDir, int yDir) 2 where T : Component 3 { 4 if (xDir == 1) { 5 transform.eulerAngles = Vector3.zero; 6 } else if (xDir == -1) { 7 transform.eulerAngles = new Vector3(0,180,0); 8 } ...
The AttemptMove
function was initially written with the integers representing the direction that the player is moving in. If xDir...