- Home /
2D game with depth, basics?
Hello!
I'm currently working on a 2D game with depth, something similar to Castle Crashers.
I'm trying to do the basics first(character moviment, collisions, etc), what I have so far is this:
My background is a Plane object (with a background image) facing positive Z. Both the character and the chest make use of a 1x1 Quad (custom mesh) with their respective image. For collisions, i added a capsule collider to my character and a cube mesh colider to the chest object. My character also has a Kinematic rigid body attached to it.
I managed to create a script for moviment and collision that works as follows:
Moving to the Right of Left = increasing/decreasing transform.X value
Moving Up and Down = increasing/decreasing transform.Y and Z values
Collision = OnTriggerEnter
So far i had no problems, but now i'm having trouble on the jump part. As you can see i have no ground on my scene(maybe I should have?), that's why my character's rigid body is Kinematic. I have tried to increase the rigidbody velocity and decrease it over time, (until my character returned to the initial Y position) but it doesn't really work...
My questions are:
Should i add ground on my scene for colision? so i could use a non-kinematic rigidbody. If yes, how can i achieve this?
If Kinematic rigidbody is the right choice, how can i script the jump?
Does the Character Controller provided by Unity has a jump funcionality? Still, it's restricted to a capsule collider :(
I hope that anyone can help.
Thanks!
You could add another plane, "the ground", perpendicular to your background one. You can disable the mesh renderer to make it invisible. Have it move up and down along with the character, but NOT when he tries to jump.
This way you can solve your problem by finding one of the many 3d jumping script tutorials.
BTW, it's incredibly easier to use 2DToolkit for something like this
Yeah, it probably is. I just don't do 2D, so my brain automatically goes "How can I turn this into a 3d problem?" :D
Great idea Em3rgency, i will try this out :) What would be best to use in my case: $$anonymous$$inematic or non-kinematic rigidbody? With a non-kinematic rigibody i had some collision issues, my character keeps bouncing when touching another collider (i.e. the wooden chest).
@Fattie This toolkit looks good, but i don't wanna pay for it(i like program$$anonymous$$g) ;)
Answer by trs9556 · Jul 07, 2013 at 08:30 PM
Instead of increasing/decreasing the velocity over time why don't you make it more static.
Set the height of the jump. Maybe like "5" units, whatever suits your needs.
From there when you hit your jump button you use lerp to move your character 5 units up, once it has reached it's 5 units, lerp it back down 5.
Using
character.transform.position = Vector3.Lerp(character.transform.position, desiredLocation.position, Time.deltaTime * speed);
When you start your jump declare desiredLocation as your current location plus 5.
Check if you have reached your destination
public float ErrorAllowed = 0.005f; //the smaller the number the more accurate
if (Mathf.Abs((character.transform.position.y - desiredLocation.position.y)) < ErrorAllowed) {
//method that goes down. Aka a bool to let you know if you are traveling up or down, and then to change the desiredLocation back to either your start location (more accurate) or current location minus 5
}
Now one thing I like and hate depending on what I'm doing is with lerp it gradually slows down. So you can compensate for this by increasing your speed. I do something along the lines of:
public float FloatForSpeedUp = 1.7f; //play around with this number.
if ((Mathf.Abs((theCamera.transform.position.y - tempPositionToGoTo.position.y)) < FloatForSpeedUp)) {
speed+= (speed*0.04018f);
}
The biggest downside to this is your FPS determins a lot of the simulated physics on this. I noticed the speed at which my object excels in the editor (on my pc) is much more dramatic then on my android phone (I use these for android) basically because my computer gets 800+ FPS while my android gets 40-60. So fine tuning around will be required.
Answer by bscarl88 · Jul 15, 2015 at 05:44 AM
What if... You have an object that is your "Shadow" that is platform controlled in an 8 directional way, this is what you control, so it moves freely around the screen in the X and Y axis. Then you have a Platformer style Player object with Graity in the Y axis. This Player object essentially uses his "Shadow" like a hoverboard. When the shadow is moving, his walk animation goes off as he is pinned to the shadow's movement. Then the player can initiate a jump and they're just landing back down on a the shadow platform. Or is this just better to do with a script and an offset?
Your answer
Follow this Question
Related Questions
Can someone help me with the jumping in this Player Controller script? 1 Answer
Jump if ground is detected 1 Answer
Delay Jumping 3 Answers
Box collider didn't jump with player 1 Answer
2D NPC should kill player on collision but does not. 1 Answer