Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Procedural Content Generation for Unity Game Development

You're reading from   Procedural Content Generation for Unity Game Development Harness the power of procedural content generation to design unique games with Unity

Arrow left icon
Product type Paperback
Published in Jan 2016
Publisher
ISBN-13 9781785287473
Length 260 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Ryan Watkins Ryan Watkins
Author Profile Icon Ryan Watkins
Ryan Watkins
Arrow right icon
View More author details
Toc

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

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at R$50/month. Cancel anytime