| Unity based 2D Character Controller March | |||
|
I've seen a ton of people struggling with implementing a 2D Character Controller in Unity. If you've tried and stuggled you know what I'm talking about if you don't then here is a quick summary. Unity is fundamentally a 3D engine, so of course it has a Character Controller optimized for 3D. This controller has a capsule collider - that means it's bottom is round. For a classic 2D platformer ala Meat Boy, Mario, Mega Man, etc you don't want a capsule, you want a block. A block that doesn't bounce, slide off edges and can balance on a platform as long as 1px of it is making contact. For NinjaBoy I hacked up a version of the Character Controller and got it working well enough to ship, but it wasn't perfect. In a few edge cases Tadeo (the game's hero) would get auto-pushed off of a platform, or slide off the edge of a platform after a perfectly vertical jump - neither would be acceptable in a classic platformer. Given that our next game is also going to be a 2D platformer, I decided to spend the afternoon building a new Character Controller, one that actually works like it should. I started by searching the web and found WS Barth's code. Although it's a fantastic jumping off point, it doen't handle the rounded bottom issue. So after a few hours I ended up with the code below. I feel it's a solid starting point for a 2D controller -- there is of course a lot more you could do to it like adding double jump, invisible step jump, etc. By the way, I am in no way the world's best programmer, so there are probably much smarter ways to write this. In addition to the source, here is a link to a Unity Web Demo of the 2D Char Controller in action. I hope you find some of the useful and of course if you find any bugs please ping me on Twitter or Facebook (links above) - thanks in advance.
using UnityEngine;
using System.Collections;
public class HeroController : MonoBehaviour
{
public float Gravity = 21f; //downward force
public float TerminalVelocity = 20f; //max downward speed
public float JumpSpeed = 10f;
public float MoveSpeed = 10f;
private Vector3 MoveVector { get; set; }
private float VerticalVelocity { get; set; }
private Vector3 LeftSideStart, LeftSideEnd, RightSideStart, RightSideEnd;
private Transform _transform;
private CharacterController _characterController;
private bool fauxGrounded = false, leftSide, rightSide;
private RaycastHit leftSideHit, rightSideHit;
private float sideHitLength = 0.7f;
private float fauxY;
bool isGrounded
{
get
{
bool ret = false;
// first we check to see if the character controller thinks we are grounded
ret = _characterController.isGrounded;
// test the left and right side of the character controller capsule
// these two tests effectively extend the sides of the capsule to be square,
// rather than rounded
//
// OPTIONAL:
// For NinjaBoy I put in an 'invisible step' which made it easier to pull off
// some of the harder jumps. Basically it means that the hero can walk in the
// air a bit and still perform a jump. You can accomplish that effect by simply
// widening the 'X' position of the left and right linecasts if the hero is running.
// If they are standing still then you keep them snug to their body.
//
leftSide = Physics.Linecast(_transform.position + LeftSideStart,
_transform.position + LeftSideEnd, out leftSideHit);
rightSide = Physics.Linecast(_transform.position + RightSideStart,
_transform.position + RightSideEnd, out rightSideHit);
// if either side is touching then we're colliding with the 'faux' ground and
// should be standing on it
if (leftSide || rightSide)
{
ret = true;
// draw lines are debugging sugar, don't need these
if (leftSide) Debug.DrawLine(_transform.position + LeftSideStart,
_transform.position + LeftSideEnd, Color.green, 0.2f);
else Debug.DrawLine(_transform.position + LeftSideStart,
_transform.position + LeftSideEnd, Color.red, 0.2f);
if (rightSide) Debug.DrawLine(_transform.position + RightSideStart,
_transform.position + RightSideEnd, Color.green, 0.2f);
else Debug.DrawLine(_transform.position + RightSideStart,
_transform.position + RightSideEnd, Color.red, 0.2f);
// reset the faux ground, we want to always be able to fall off an edge
fauxGrounded = false;
// if we moving downward, then we want to snap the hero up, rather than
// letting them slide down the edge of a platform because their collision
// really has a rounded bottom
if (MoveVector.y < -0.1f)
{
if (leftSide && rightSide) fauxY = rightSideHit.distance <
leftSideHit.distance ? rightSideHit.distance : leftSideHit.distance;
else if (leftSide) fauxY = leftSideHit.distance;
else if (rightSide) fauxY = rightSideHit.distance;
fauxY = (sideHitLength - fauxY) - 0.17f;
fauxY = _transform.position.y + fauxY;
fauxGrounded = true;
Debug.Log("rt: " + rightSideHit.distance + ", lt: " +
leftSideHit.distance + ", fauxY: " + fauxY);
}
}
else
{
fauxGrounded = false;
}
return ret;
}
}
// Use this for initialization
void Awake()
{
_characterController = gameObject.GetComponent("CharacterController") as CharacterController;
_transform = _characterController.transform;
LeftSideStart = new Vector3(0.5f, 0, 0);
LeftSideEnd = new Vector3(0.5f, -sideHitLength, 0);
RightSideStart = new Vector3(-0.5f, 0, 0);
RightSideEnd = new Vector3(-0.5f, -sideHitLength, 0);
}
// Update is called once per frame
void Update()
{
checkMovement();
HandleActionInput();
processMovement();
}
void checkMovement()
{
//move l/r
var deadZone = 0.1f;
VerticalVelocity = MoveVector.y;
MoveVector = Vector3.zero;
if (Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone)
{
MoveVector += new Vector3(Input.GetAxis("Horizontal"), 0, 0);
}
}
void HandleActionInput()
{
if (isGrounded &&
Input.GetKeyDown (KeyCode.Space))
{
jump();
}
}
void processMovement()
{
// transform moveVector into world-space relative to character rotation
MoveVector = transform.TransformDirection(MoveVector);
//normalize moveVector if magnitude > 1
if (MoveVector.magnitude > 1)
{
MoveVector = Vector3.Normalize(MoveVector);
}
// multiply moveVector by moveSpeed
MoveVector *= MoveSpeed;
// reapply vertical velocity to moveVector.y
MoveVector = new Vector3(MoveVector.x, VerticalVelocity, MoveVector.z);
// apply gravity
applyGravity();
// move character in world-space
_characterController.Move(MoveVector * Time.deltaTime);
if (fauxGrounded)
{
_transform.position = new Vector3(_transform.position.x, fauxY,
_transform.position.z);
}
}
void applyGravity()
{
if (MoveVector.y > -TerminalVelocity)
{
MoveVector = new Vector3(MoveVector.x, (MoveVector.y - Gravity * Time.deltaTime),
MoveVector.z);
}
if (isGrounded && MoveVector.y < -1)
{
MoveVector = new Vector3 (MoveVector.x, (-1), MoveVector.z);
}
// this handles head hits, you can tweak the 'Y' value to have a slower of
// faster decent a value of -1.0f, felt right to me
if ((_characterController.collisionFlags & CollisionFlags.Above) != 0)
{
MoveVector = new Vector3(MoveVector.x, -1.0f, MoveVector.z);
}
}
public void jump()
{
if (isGrounded)
{
resetFauxGround();
VerticalVelocity = JumpSpeed;
}
}
private void resetFauxGround()
{
fauxY = 0;
fauxGrounded = false;
}
}
|
|||
|
|||
| The Imp Sept | |||
|
Winged guardians of Master Sentaro's Tower. Taking many forms, these devious little suckers will stop at nothing to protect their master's home.
Sentaro's Tower will be the first endless area added to NinjaBoy. We'll be taking lots of inspiration from the classic "3 lives" style games, but putting a modern spin on things. The Imp is the first of several monster concepts we'll be revealing over the next few weeks. Also planned for the update: Game Center, tons of new Easter Eggs, new Ninja Suits, an unlockable Hero, and if time allows a new Story Area. If you haven't already checked out NinjaBoy jump over to iTunes and grab it. Our sales suck :) but reviewers love it, so help spread the word!
Indie Game Magazine 85% |
|||
| NINJABOY SHIPS! Aug | |||
|
We put a ton of effort into this version of the game. Moving to iOS meant a completely new game. We added 30 new levels, redid all the art, animations, sound fx and music. Reworked much of the game, pacing and level design. And, added a fantastic new shop, where you can spend money earned after each level to buy ninja suits, trinkets and potions. It's everything from the WP7 version that got us 190,000 downloads and an average rating of 8.7 / 10 from 4,000+ users, and then way way more :). |
|||
| TRAILER - NINJABOY iOS! July | |||
|
With any luck NinjaBoy will be available for iPhone and iPad world wide in the next few weeks. Check out the trailer below.
NinjaBoy is a puzzle-platformer. It's about avoiding traps, collecting stars, solving puzzles, and out-smarting enemies. Guide young Tadeo through all 80 levels and defeat Lord Hito. Challenge yourself by avoiding all the traps, sneaking around all the enemies, mastering Dragon Chop, and gathering all the stars to unlock the Yellow Jumpsuit. Strive for elite gamer status by earning 80 Grandmaster Badges and unlocking the Shadow Guise Ninja Suit. The Kingdom has fallen. Lord Hito now rules with an iron fist. During the battle, Master Minoru and his youngest pupil, Tadeo, managed to escape. Hidden away in a forgotten temple, the young ninja trains in hopes of defeating the Dark Lord and restoring freedom to the Kingdom. |
|||
| NINJABOY iOS COMING SOON! July | |||
|
I know we've been very silent for several months, but I'm here to say we're back and couldn't be more excited about where we're at. NinjaBoy, now just under 200,000 downloads and 5,000 reviews on Windows Phone 7 is coming to iPhone and iPad! This is our first iOS game and we really tried to step-up our game for the very competitive platform. NinjaBoy iOS is a complete rewrite of NinjaBoy WP7. Everything from the in-game art, to level design, to the monsters have been reworked. We now feature now a rich story complete with cut scenes, in-game magic shop where you can use gold earned after each level to buy ninja suits, potions and trinkets, 80 carefully crafted levels, voice overs, a new sound track, completely new sound effects, and ninja rankings for each level. We poured a ton of effort into this version of NinjaBoy and really hope you enjoy it; below are some in-game screen shots. In the next few days I'll be posting a gameplay trailer as well as a dev diary on the journey of going from WP7 to iOS, the tools we used, etc.
Before I sign off, I just wanted to say thanks to everyone that supported us in making this release happen. A special thanks to all the Windows Phone 7 fans, whose feedback has been invaluable, Mika Mobile for pointing us in the direction of the amazing Unity 3D, Rocket 5 Tutorials for helping us build our first iOS platfomer, our friends and family that did such an amazing job with game testing, sound effects, and music, and, the incredibly helpful community of indie gamers that make it possible for two brothers to realize ideas on their own. One last note to our Windows Phone fans. As soon as Unity supports Windows Phone we will make sure the new and improved NinjaBoy is available for you guys. |
|||
| CONCEPT ART - SPACE COMMANDER older | |||
|
Check out some of our concept art for our next game Space Commander. We're confident that in-game visuals will be as good if not better than the concept art, but we can't officially claim that yet, because we haven't started writing code :). Below is the concept work for our hero, to see a fuller set of concept art, including level mockups, check out our In the Works section. Enjoy!
|
|||
| PROGRESS UPDATE - NINJABOY older | |||
|
Wanted to let you guys know how we're progressing on our NinjaBoy update. Firstly, it's gone very slowly, our day jobs have been insane. One of us had 6 months of required overtime and 6-7 day work weeks trying to land our company's latest product. In addition, we continue to learn about and evaluate alternative technologies and platforms for our games. At the end of the day there is still very little money to be made on WP7. It's sad but true. We love the phone and still believe it has lots of potential, but it hasn't grown nearly as fast as we'd hoped. As a result things like iOS and Android become more interesting. We have currently landed on using Unity for porting NinjaBoy and future projects. We'll let you know how it goes and hope to get back to NinjaBoy WP7 soon. Cheers. |
|||
| SNEAK PEEK - HOWLING RISE older | |||
|
We're happy to say that our work to find other revenue opportunities has paid dividends and we're very close to announcing the availability of NinjaBoy on platforms beyond WP7. Expect to hear something about that effort in the next few weeks. Although we've still got a fair amount of work ahead of us before we finish NinjaBoy's next WP7 content update, we wanted to let you guys know what we're up to. Since you've been so great about downloading, reviewing and sharing NinjaBoy, we felt it was only right to give you an early preview of what to expect with our next update. The aside image provides a sneak peek of Howling Rise, the first of two new adventure areas we have planned. In addition to all new art, Howling Rise will feature two new enemies, a new ability for Tadeo, and what we hope are a great set of challenging but enjoyable puzzles. Like the original two areas, Howling Rise will be home to 25 unique handcrafted levels, and like Hito's Yard the final level will pit you against the master of the area in a challenging boss battle. Although we don't want to reveal too much at this point, we did want to assure you that we're putting a lot of energy into this update, expect a ton of exciting new gameplay mechanics and puzzle elements. In Howling Rise many of these mechanics will revolve around the dark arts, shadow magic and necromancy. Taking center stage will be our two new enemies the Necromancer and Ethereal Ninja.
Aside from Howling Rise, the next content update will feature a fourth area which we're pretty far along on, but aren't ready to talk about just yet, as well as some other new gameplay experiences that we hope you'll really enjoy. I'd love to give a firm date as to when the next update will ship, but that just wouldn't be fair to anyone. Trust that we'll work as hard and as fast as we can to deliver an expeirence that live's up to the 2 Ton name and we very much hope you'll agree was worth the wait. Cheers! |
|||
| LEVEL UP? NINJABOY REACHES 100k DOWNLOADS older | |||
|
Just as we annouce our sneak peek of new content, NinjaBoy hits 100k downloads! Thanks to all our fans for getting us to this very cool milestone. Cheers! |
|||
| BLiTTER + TWiSTED INTERVIEWS US older | |||
|
An exclusive interview with the very cool and comprehensive tech, gaming and entertainment blog BLiTTER + TWiSTED has just gone live. Learn about my favorite games, how I got into gaming, my first cell phone, WP7 development and my thots on XBL :). Cheers and a special thanks to BLiTTER + TWiSTED for contacting us and being so darn great about putting it together! |
|||
| NINJABOY ON SALE older | |||
|
We're putting the paid, ad free, version of NinjaBoy on sale this weekend. You can now get it for 99 cents on the Marketplace. At the end of the day we think 99 cents is the right price for this and most of the other WP7 games, but the higher priced XBL tiles strongly encourged us to price it higher. Rather than play the if you can't beat 'em, join 'em game, we're going to sell it for 99 cents and see if we can't start a bit of a trend :). Cheers and enjoy! |
|||
| NINJABOY REVIEWED - WINDOWS PHONE DAILY older | |||
|
NinjaBoy just got a glowing 9/10 review from Windows Phone Daily. A big thanks to this up and coming WP7 blog for taking the time to not just review the game, but play it from start-to-finish so they could fairly evaluate all of the hard work we put into it. |
|||
| NINJABOY HITS 30k DOWNLOADS older | |||
|
|
|||
| NINJABOY - CLOSE AFTER LEVEL COMPLETE older | |||
|
The source of the bug in NinjaBoy that causes the game to close after you complete a level has been located. It has to do with region and language settings. If you switch to en-US the issue goes away. This has now been confirmed by 2 players in different non-US countries. I realize this is not a true fix, but at best a workaround. The bummer is there is little I can do about it - nothing in my code is region specific :|. I'm pinging Microsoft everyday to try and get some help here, but have yet to make much headway. As soon as I have a real fix or update from Microsoft I will post info here. Again, so sorry for the hassle and thanks for understanding. Cheers! |
|||
| NINJABOY REACHES 10k DOWNLOADS older | |||
|
Just a quick post to thank everyone for getting NinjaBoy to 10,000 downloads in just two days! We're very happy that so many people are enjoying NinjaBoy and look forward to deliver heaps of great content over the next few months. Cheers! |
|||
| NINJABOY HITS 1,000 REVIEWS older | |||
|
As indie developers reviews are really the best way for other people to see our hard work. To date we've made almost no money on our games, so we can't afford things like marketing. Any marketing we do get comes as a result of our fans sharing with friends and giving us reviews. Again, thanks so much, we really appreciate it and hope to deliver more great content in the near future! |
|||
| NINJABOY RATED #2 OVERALL older | |||
|
Earlier this week NinjaBoy made it all the way up to the #2 rated game on the entire phone (games or otherwise) based on Bing Visual Search for WP7. This truly blew us away! To make it that high after less than a week on the marketplace is just so cool. It really lets us know that what we're doing is truly valued. Thanks so much to everyone that has reviewed, downloaded and shared NinjaBoy with others. We put a ton of work into this one and it's really great to see it so well recieved. |
|||
| NINJABOY SHIPS older | |||
|
NinjaBoy is in the marketplace! We're especially proud of our work with this one. Our goal was to make a ninja game that focused on the non-combat side of ninjadom :). Sure NinjaBoy still has combat elements, but we feel its obstacles, monsters, platforming and puzzles are it's best attributes. We really hope you'll agree it's a good twist on things. You'll notice that the first version ships with two areas. The first, the "Dojo" intros you to the core gameplay mechanics. "Hito's Yard" (the second area, unlocked after getting 70+ stars) showcases the richer gameplay mechanics, specifically with monsters, that we hope to continue in future areas. Over the next year we will release many more areas, and look forward to unfolding the entire NinjaBoy journey for you guys. |
|||
| AKIAK v1.4 older | |||
|
Akiak version 1.4 just shipped! We've added a new course, Easter Forest as well as a new present, the Swimming Fin, to the classic Arctic Dash course. v1.4 also sees us switch to in-game Ads - rather than just at the gameover screen. After much consideration, we've decided that this is the best way to monitize Akiak. We hope you agree that displaying Ads in-game while still allowing it to be free is a good way to go. Of course we'd love to hear what you think about this change, so feel free to mail us at feedback@2tonstudios.com. Thanks! |
|||
| AKIAK HITS 1,000 REVIEWS older | |||
|
We're proud to announce that Akiak hit 1,000 worldwide reviews earlier today, with an average review score of 8.7/10. Thanks to everyone that gave us a review, they help a lot and let us know we're heading in the right direction with our games! |
|||
| AKIAK HITS 20K DOWNLOADS older | |||
|
We're happy to say Akiak passed 20k downloads this week. Thanks to all of you that supported us and keep spreading the word... maybe we can get to 40k :). |
|||


After 8 hard months of work, NinjaBoy has passed the Apple review process and is now available world-wide on iTunes!

After getting swept away by our day jobs and working hard to explore other revenue opportunities for our games, we're back to focusing
on our next content update for NinjaBoy.
As many of you know a necromancer is a master of the undead and spirit world. His ability to
manipulate the unseen world around him will factor heavily into the types of challenges and opportunities he poses to Tadeo.
The Ethereal Ninja is the second new enemy and we're very exicted about what
he brings to the experience. Of physical form but powered by the spirts of the underwold, he blurs the line between living and undead. This ability to
be a "living ghost" opens up the possibilty for puzzles and gameplay mechanics that we think will be very cool.
The new numbers are out and NinjaBoy just hit 30,000 downloads!
Thanks to everyone that has downloaded, shared and reviewed NinjaBoy. Getting to 30k at all is fantastic, making it there so quickly
really means a lot. So again, thanks!
Thanks so much for all the great reviews! This morning we hit 1,000 worldwide reviews
for NinjaBoy with an average rating of 9.2/10. Hitting this in less than a week
really makes us proud.
The accolades for NinjaBoy keep pouring in and it's all thanks to our fantastic fans!