Here’re some questions for the crew here at the forum - maybe someone has already solved this little issue…
How do you script the kind of gravity you might encounter from a space game - essentially, when you get close to certain things (planets, asteroids, etc) you get pulled towards them.
I’m not talking about simulating orbital mechanics or anything that complex, just something as basic as scripting a distance based trigger that causes colliders to accellerate towards the center of a given mesh, regardless of their facing or other orientation in regards to that mesh.
Even, perhaps, an FPS situation where a player could literally run ‘around’ an asteroid?
Any ideas?
(also, can Unity directly affect the vertices, etc of a mesh? Morph it somehow beyond just basic scaling and position changes?)
I only have Indie - so I’m out of luck on any solutions that require Pro.
at the end of the thread joe gamble posted a script he said works for what he needed. i didn’t try it. iirc it’s distance based rather than gravity and it doesn’t take into account normals - so your character won’t follow the slope of a hill.
in the same thread, i posted an incomplete script that takes normals into account and should work for following the surface of an odd terrain. it uses force to fake gravity towards the center of the object. mine doesn’t work below the equator though. vector3.up doesn’t want to look down. haven’t had a chance to get back to it though. so you’d need to tweak…
And just to make it clear, this is all possible with Unity Indie.
Unity Pro is really cool, but mainly adds graphical goodies, low-level extensibility (raw OpenGL calls, C++ extensions)… and of course making games for Windows.
To calculate gravity that is distance dependent you can use the following formula:
Fg = ( G * m1 * m2 ) / (r * r)
where:
m1 is mass of object 1 in kg
m2 is mass of object 2 in kg
r is the distance between the 2 objects
Fg is the force of gravity between the 2 objects
G is the gravitational constant