- Home /
How to make objects standing on other objects.
Hi there!
I'm trying to make a 2D game with newtonian physics: objects are not dragged down by gravity, they all attract each other in the world space. This part works perfectly fine, i have two massive planets quietly orbiting around an even more massive sun.
Now that i have a little system nicely orbiting and all i'd like to add some objects on the planets, including life (the player) and a tons of other things to make these planets playable. I made a little controller script attached to a simple square that allow the player to move the square left and right. the controller works as i want but there's my problem:
At some point if you move the square in some direction, it will start to roll because of it's collisions with the ground. if you're lucky it will stop rolling with it's y axis pointing "up" (the minus of the y axis being toward the planet) but if you're not it will be renversed so the x axis will be pointed "up". So telling your controller to go left can make your object take off from the planet in this situation.
I didn't do it with a character but the problem would look like that: moving the character will eventually make it fall (as you can't use fixed angle on the rigidbody) and stay on the ground laying on his back, then, using the controller to move him again will make him start to walk toward the sky and leave the planet because the force is added on his x axis.
The solution is quite easy to find: the controlled object needs to keep a rotation that always make it "stand on his foot", the y axis being: minus = the ground, plus = the sky. It's easy to think but i couldn't find how to apply that in Unity through scripts or anything else.
I tried several things but i'm quite lost with rotations in Unity and i'm slowly losing motivation as i try new things that aren't the solution to my problem so i thought it was time to ask!