Showing posts with label scripting. Show all posts
Showing posts with label scripting. Show all posts

9/25/2012

Finetuning character movement

At the end of Iteration 001, the character movement felt odd in more then one situation:

  • Character got stuck at the of blocks
  • First jumping and moving while in air did not work
  • Character "floated" along ceilings while jumping
  • Movement did not ease out but came to a sudden stop once the controller was released
Let's start with this problems caused by "not reading the manual"... again.

When the character got stuck at the edge of a block it looked like this:


A close look in the scene showed, that regarding Unity everything was quite in order. The character controllers capsule rested savely on the edge. However the little blocky man refused to move even the slightest bit to the right.


The solution to this problem was to simply adjust the "step offset" property. A higher offset allows the character to climb steeper steps. So movement to the right was blocked because the default virtual leg length of one was far to short for my scene. More information on can be found in the Character Controllers API docs.

A step offset of 10 works quite well
The rest of my evening was consumed by finetuning the PlatformerController.js borrowed from the 2D Gameplay Tutorial. 
  1. In air controls did not work as I expected. I added two additional properties in order to set a minimum and maximum in air velocity:

  2. Collision with a block above did not end the current jump as vertical speed was not set to zero. The inversion of vertical speed on collision did the trick:

  3. My first iteration on character animation did not include the adaption of animation speed based on movement speed. I fixed this writing two additional lines of code and once again loved unity for it's simplicity:

I end todays post with one of my favorite quotes:

Dans ses écrits, un sàge Italien
Dit que le mieux est l'ennemi du bien.
~Voltaire

Having said this, character movement feels good enough for me for today :)

9/04/2012

Fade Orthello sprites from solid to transparent

Todays task seems to be fairly simple: I want to put a logo sprite at the beginning of level one and fade it out as soon as the game starts. Placing the sprite is not a problem but finding the way to adapt transparency using Orthello is not straight forward... at least if you do not bother reading the manual :)

My first guess to grab and slide the "Alpha" property produced a warning:
Orthello: Alpha value can not be set on this materialReference!
Sprite material properties
Unfortunately googling for "Orthello materialReference" did not (obviously) produce any useful result. Just the well known "Did you mean: othello ...".


Note to self: Never name anything within the reach of one Levenshtein distance of any popular word if you do not want to loose customers due to Googles suggestions.

Ok, then I'll trial and error my way towards success:
  • Unticking the "transparent" checkbox → Ugly black borders where the transparent sprite sections used to be
  • Ticking the "Additive" checkbox → Sprite color is added on top of background color
  • Changing the "Tint Color" → Another warning
Finally replacing "Material Reference" transparent with the word alpha did do the trick. As I found out afterwards, there is a quite good explanation on the first page of Orthellos manual which also happened to be the second google hit above...

Lessons learned: RTFM (at least page one) even if it's late in the evening!

In short: There are named material references which can be assigned to any sprite. It's also possible to define your own material reference. The one named "alpha" modifies the sprites transparency according to the "Alpha" setting as well as the color tint according to the defined "Tint Color". I left the tint color blank, i.e. used a medium gray, and was now happily able to fade my sprite in and out using the "Alpha" slider.

Creating the script to do the fading was no real problem compared to understanding the property handling:

7/21/2012

Controlling the character

Next step ahead is a controllable player character. Part three of Tims tutorial explains how to add an animated sprite to Unity using the Orthello 2D framework. So it's once again time to start with some graphics. Hours later I ended up with a sophisticated walk animation cycle:
Mother of all walk animations
Now it's finally time for some coding. Unfortunately I did not manage to get the tutorial script running for my level design. I do not use one dedicated block as bottom border but instead wanted to build my level using many sprite blocks put in line with each other like this:
Player standing on two layers of blocks
What the tutorial script does is the following:  Cast two short rays downwards, one on each edge of the sprite, and check if a ground block was hit. If yes then the falling state is set to false and player is positioned on the ground:

But as there are two layers of ground in my level the sprite kept falling after being positioned on the ground and ended up walking underground.

I spent some frustrating hours trying to figure out a solution using the distance to the ground below or above (if already underground), and finally had to give up for the night.

The next evening I tried something different. Why should I even bother casting rays if Unity already offers a full blown collision system. Yes maybe this is overkill for 2D gaming, but I desperately need some success.

I ended up using a Character Controller and some input from the 2D Gameplay Tutorial to make my little red fellow walk and jump... finally: