The Illusion of Progress

I thought I had run out of things to talk about by this point, but of course by given how creative I am, there’s at least one more thing worth writing a blog post about.
Creativity doesn’t equal great so I’m not certain if this’ll be an interesting read, but let’s see how my blog writing can save it!

In Spirits of The Shogun when outside of fighting, the background scrolls with a parallax effect whenever the player pushes against the right boundary. When he/she stumbles upon enemies, the scrolling stops and the player is forced to fight the inbound wave of enemies. This effect is used to give the player an illusion that he really walks through a level containing a grass field and fight through waves of enemies.
What really happens is just the player making a ninja trying to exceed the right boundary and gets relocated to its horizontal position by doing so. However, a script inside each part of the background notices when this happens and tells each part to move at a speed. Each part moves at a different speed and that creates the parallax effect.

I’ve been through this briefly before in an earlier post but to new readers, when the player has traveled a certain distance (a value being reduced until 0, whenever the player pushes the right boundary) the apparent progression through the level will cease.
In the travelling phase, the right movement boundary was set on the right side, near the center of the screen. A good placement so the player wouldn’t be too near the edge when enemies appear.
However, in the fighting phase, the right boundary is set to the very right edge of the screen, meaning the player has more space to move around. That means a lot for the player since he/she has to dodge the incoming enemies and their melee/ranged attacks.Blog5Pic1.png

One question struck me hard though. If the traveling boundary was shorter than the fighting boundary, what happens when the player is exceeding the travelling boundary after the wave is finished?
The code would naturally make the player teleport back to the boundary, nothing wrong there. It’s just that it would certainly not look too good, and what does code know about beauty and grace?

I had to make some sort of smooth transition and that particular word gave me an instant solution. I would have the player simply slide back into place again instead of being teleported. No movement towards the right could be done while the player was sliding back. Thus no distance could be covered by exceeding the boundary and push it.
Also when the player slides back, I’d have to make the background scroll as well, to make it look like the ‘camera’ focuses back on the player, who’ll proceed through the level.
This transition had to be quick, because the player will be waiting to move again, so when the background scrolls while the player is sliding back, it will scroll much more faster.

The easiest part with that solution was to have a relaxing time with nothing more than just adjusting the speed of the player’s transition to the boundary and the speed of the background. I would have to make it look plausible too.

I don’t know if the implementation of the solution was done right, because I ended with two booleans that checked when the player pushes right. One that makes the distance left to the next battle being reduced and one that makes the background scrolling occur. I did have to split up my original boolean though, so the player couldn’t ‘cover distance’ while sliding back.

I think the special part with this is that we had no sort of camera that followed the player as he/she actually moved through a still image with occasional stand-stills as enemies appears. I don’t think that would have been the right way to create our game either. What it really is about is to create the solid experience you’d like the player to enjoy, no matter how you do it… It’s just that some ways does it better than others.

The Effect of Crossfading Soundtrack

This has been a cool addition to the game!
The soundtrack changes depending on which realm the player currently is in. If the player is in the human realm, asian instruments and taiko drums plays. If the player is in the spirit realm, the soundtrack goes all electronic and choirs are heard singing.
By the time I had to implement this dynamic effect, the gameplay themes of the game were nearly done, but still usable as they were. In fact, these uploaded tracks aren’t completed either but… time constraints.

In the human realm:

In the spirit realm:

I wish I could’ve done more before uploading them, and I most probably will.

I put these soundtracks into each of their own audio source in a game object on the scene, and assigned them to a script. I made sure the spirit version was at 0 volume, since the player would begin playing in the human realm, not the spirit realm. Now I had to make these two tracks crossfade when the player switches realms.

The script I assigned the audio sources to is named SoundManager (more appropriately named as AudioManager) and it manages the crossfade effect as well as playing sounds.
It has the boolean variable m_currentRealm that is being set to the same boolean value that decides which realm the player is in (LevelData.m_spirit). In each update, these two values are checked if they’re equal to each other and if they aren’t then it means the player is in another realm and the soundtrack needs to change.

A coroutine function that creates the crossfade effect receives different parameters depending on what realm the player is in. A coroutine, as explained in a previous post, executes a code that runs alongside the main one, so the program doesn’t have to wait until that code has fully exectued.
The parameters given is of course the audio sources so the coroutine knows what volume of an audio source to lower and vice versa.

Before lowering and raising any volumes, a float is instantiated with a value of 0f. It acts as a counter (timeCounter).
Next, a while loop runs as long as timeCounter is not similar to the value 1f, being done with Math.Approximately(). In the beginning of this loop, the timeCounter is being added with 0.2f.  Then the volume of the audio source that fades away, gets reduced from it’s maximal value (1f) with the new value of timeCounter. After that, timeCounter gets assigned to the volume of the audio source that fades in.
A brief period of time follows by using ‘yield return’ and then the loop repeats.

When timeCounter is similar to 1f, the coroutine proceeds to assigning a couple of values. It first of all makes m_currentRealm equal to LevelData.m_spirit so the SoundManager is updated on where the player is and then it makes the boolean value m_changing go false again.
I never mentioned m_changing. All it does is making sure several coroutines doesn’t start executing. It only does that when m_currentRealm and LevelData.m_spirit is unequal and m_changing is false. If that if statement passes, m_changing goes true.

That’s what is being done when a crossfade effect occurs. I did not come up with this by myself, and searched around the web in how to create the effect. Nonetheless, I’m quite satisfied to have achieved making the soundtrack dynamic and responsive in this kind of way and it’ll surely make the game more immersive for the player!