I am developing a space game and need information on how gravity works in Unity. Can you establish gravity on local objects? What I am envisioning is a system where players can walk around the millenium falcon, for example, while it is twisting and turning in space without falling to the ceiling. Is this possible?
I’m not entirely sure what your intention is, but from how I understand it, I’d turn off gravity on the rigidbodies (as you’re in space). Then on anything that will be moving along with the rotating object add a constant force (Component → Physics → ConstantForce) and add a -Y relative force to that component to simulate artificial gravity. As long as it stays rotated with the parent object, it will then have a negative force applied along its y-axis (ie, be pushed “down” in its own coordinate system). Is that what you’re looking for?
Unity’s gravity occurs in a single direction so it’s not really applicable (in an easy way) to games/content that require gravity to point different directions. Like when walking around a sphere for example, to start the gravity points in the -y at the north pole, but it then points in +y at the south pole, and some mix of x/y/z in between. You can try what burnumd suggests, or ignore the parenting and whatnot and just have a “heavy enough” gravitational force that you manually apply (AddForce) each frame, that force vector going from the object being “pulled” to the center of the surface object.
You can do fun things with your own gravity calculations, like this (a to-scale, except the radial size of each body, of the inner solar system, the Sun out to Mars).
That’s definitely a workable solution, and it’s probably what I’d do for more a realistic feeling simulation. Will that work if the ship twists in different directions or the gravity is intended to pull in some direction other than the center of mass? I guess what I was thinking of is something like 2001: A Space Odyssey*, where a relative -y constant force would simply push the player out towards the floor (and then you just have to make sure the feet are oriented correctly). I’m still not sure of the OP’s intent (if we’re close enough in to an occupant of the ship, how will we even know what the ship’s orientation is in the larger world), but that would probably determine how it would be handled.
*I know the Discovery uses angular momentum to generate artificial gravity but I figured we were using the Michael Bay mindset of ignoring cause and effect for the sake of looking cool.
That’s all up to you as you have full control over the direction in which to apply your own gravitational force. The pivot point/center of mass is an easy example but certainly not the only way to go about things.
You guys keep talking center of mass and I’m not sure if I’m envisioning this properly. Say you have the millenium falcon again as an example again…the ship is smaller but flat. If the ship flips upside down based on what you’ve said so far will the players(multiple) stick to the floor or is that going to be relative to the center of mass? What if the ship is huge like a star destroyer?
The general point to take home is that it really doesn’t matter. “Gravity” is simply an attractive force between two objects based on their relative masses. There are a number of ways to fake this force to make it point in whatever direction you want. It’s maybe best not to think of it as gravity at all, since the Millenium Falcon really wouldn’t have enough mass to have any noticable effect on Luke, Han, Chewie, et al (just like the space shuttle/station doesn’t exert an (easily noticeable) force on our astronauts). So there must be some fictional construct on the ships in Star Wars that holds people to the floor.
Think of it more like a magnet. You define some point or plane on the ship and tell it to start pulling anything that comes near it. Then whatever orientation the craft takes in space has no bearing on its occupants. The “gravity” applied by clicking “Use Gravity” in the inspector is just a shorthand for a force applied on the world’s negative Y-axis because it’s convenient for any game that takes place on a planetary surface. Since you’re taking your game to outer space, turn that gravity off and apply your own forces based on what you think looks right. It will likely take a lot of twiddling with mass, force, and position values values until it gets where you want it, but it’s very doable.[/b]
Isn’t it simple enough to apply a force to the player along the ship’s negative Y-axis (assuming that’s towards the floor)? That way, no matter whichever way the ship rotates, the person inside will still be experiencing gravity in the direction of the “bottom” of the ship.
Now, it gets trickier with realistic artifitial gravity (not what the Falcon uses), which requires a constant rotation of the ship. In that case, you’re probably looking at adding a constant force along the same vector as the direction of the floor from the center of rotation. That’d be faking it enough to still simulate an approximation of real artificial gravity (centripetal force).
Am I way off on this?
Sure, that could work just fine. But that again just proves the point, it’s up to you the developer to sort out where you want gravity to point and then make it so! It’s a physics engine, you can calculate and apply forces, whether those are in fixed directions or always changing or ???, it’s all the same.
You channeling Jean Luc there?
This example doesn’t seem to work with the 2.51 plugin under winXP. There’s a short flash showing maybe a planet and then the four windows are black and the info in the left down corner is running. But nothing inside the window. Any ideas? Aren’t the plugins backwords compatible?
Should be as that was only published with 2.5.0 so it’s not like it’s even an old build or anything. I could swear I gave it a test look with a pre-release build as well. Anyone else having that sort of trouble? If so then PM me right away and I’ll see what’s up (content bug, player bug, both, neither, other, etc. ).
I did have an idea of taking the classic text book Einsteinian visualisation of a ball rolling along the fabric of spacetime almost toungue in cheek literally to fake a space and planetary orbit system using non-rendered terrains and tracking them in the same way as normal … of course it would only be a 2D / 2.5D? only solution unless one could envisage a way to track different terrains at a time; at which point my head started to ache! As the classic ball falling into a ditch at the right speed and then a cheating bit of code to enter exit “faux orbit” it could be a quick fix for a top down or isometric type gameplay perhaps?