Making My Little Bat Jump in Unity

Nick Lunt
RE: Write
Published in
6 min readMay 4, 2020

Learning how to use interaction methods with characters in Unity (Class Project #2)

So, I’m doing these blog posts a little bit out of order in the process of finalizing things. I’m actually going a bit backwards and using the assets I made for my final assignment for this project. The post in which I describe my process of making these assets and animations for the final project can be found here: link.

For this assignment, we were tasked with creating a basic scene in Unity with at least one character and background elements, and that this scene would somehow change in reaction to some kind of user input, either a finger touch or a button press. And that this interaction could be tested on a mobile device.

Using the bat character and moving background from my final project, I decided that I wanted to get my bat to do something on command, with the simplest action he could do being a jump, without having to create more animations and assets.

I followed the tutorial below heavily and would recommend it as being very easy to follow and understand. Over the course of this semester, I’ve watched many Code Monkey tutorials and have found them very helpful.

I found it easy to understand just what each line of code was doing, and though I did still have to look up a few things when I ran into errors not shown in the video, for the most part everything worked.

The above gif shows one of the very first steps in the process, where I was able to add a RigidBody aspect to my bat, which causes him to be effected by gravity. However, at this point there technically wasn’t any ground for him to hit so he just kept falling into the void off screen. To fix this, I simply added a little platform under his feet which would be invisible in the final game.

Once this platform was in place, I followed the tutorial’s code example and altered it at points to fit my particular scene. I set the input to be a press of the spacebar, which would have the bat jump a certain distance into the air. It took some time playing around with the values of just how high he should jump and how much gravity should be pulling on him, as shown in the gifs below, but I finally ended up with a result that looked like a realistic jump height and fall rate for the character.

This was attempt one, where the jump height was set way too high.

I then accidentally set it to be even higher, accidentally yeeting the little bat out of frame with every jump.

This was the third attempt, where I wound up taking the settings too far in the other direction, causing him to be extremely heavy and only jumping a few inches off the ground. Also shown in this gif is another problem that needed to be fixed, where you can see the little bat just keeps rising and rising. What’s causing this is me pressing the jump button, even before he’s returned back to the ground. Pressing the button quickly would allow him to just keep jumping until the user finally stops pressing the button, allowing him to fall back to the ground.

This issue was addressed in the tutorial and Code Monkey did show a method of coding which fixed the issue, using a raycast from inside the character to see if it was on top of the platform or not, and not allow jumps unless it was directly on the platform. I wasn’t able to get this to work, however. I followed the process but a key setting toggle wasn’t showing up for me in my version of Unity so I decided to leave the infinite jump in there, since this was just a test project.

To the left, you can see the end jump result, before I moved onto the next step and tweaked the settings again.

After I found myself feeling confident with the jump settings, I decided to add in another element to make the scene more complete. I added in a rock obstacle for my little bat to jump over. This asset I didn’t create myself, instead just pulling it off the internet as a png file of an actual Animal Crossing rock, but for this test that’s all I needed. Everything else in the scene was drawn and created by me, though.

Placing the rock in the scene, I animated it similarly to how I animated the scrolling background, going key frame by key frame to steadily move it along.

This had to be worked with, of course, as the first attempt saw the rock visibly zooming back to the beginning of the scene to repeat its animation cycle.

In the end, I was able to create a smoother transition between the rock disappearing and reappearing in scene, though it can still be seen falling into frame again on the left. Beware of falling boulders, LOL.

I was happy with this and was excited to take the next step with prototyping the scene on mobile. I knew from previous attempts that I was not going to be able to use xCode, as I had been having problems with the connection between my tablet and my computer. I decided instead to use the Unity based app ‘Unity Remote 5’ to test run the scene directly out of the program.

I ran into the same connection errors though, with my tablet and computer not communicating correctly with each other, no matter how many different charging cables I tried. I managed to get it working once, just once, and am so grateful that I took pictures of it while it worked.

Below is the final end result. I was extremely excited to see it all come together and I am very encouraged to keep working with this project, even after this semester, to see if I could make an entire game out of my little bat character and his simple animations.

In order to change the input from “pressing the space bar on the keyboard” to “touching the device screen”, I used the beginning of this tutorial to change the line of code relating to input.

--

--