In the spirit of Mario Galaxy, I’m trying to replace the first person controller with a rigidbody that gravitates (and aligns) towards arbitrary objects.
Here’s what I’ve got so far (make a big sphere, stick something slightly above it, attach this script to it, and set its attractor to the sphere – you should be able to zoom around on the sphere):
Edit: the code posted here has been fixed – see below. I’ve removed the guts of this post so that no-one inadvertently uses the old, highly borked code.
Here’s where I’d like help:
Edit: note that these issues are all resolved in the code posted below.
It’s very hard to move without jumping if the terrain isn’t smooth. How does the character controller overcome this without flying the rigidbody into space at rather slight provocations.
This is subtler, but more annoying. I notice sometimes as I’m running around my little planet that my direction sometimes drifts slightly. But the WORST thing is that when I approach the south pole, I spin.
My knowledge of Quaternions is pretty poor, so I’m obviously doing something wrong.
I’d really prefer to adjust my up vector more gradually in the face of sudden changes of gravity (it’s fine when the attractor doesn’t change, obviously, but it will suck if I switch attractors suddenly as in a certain Wii game). I’ve been messing with Slerp but … messing is the word and the result.
(I also want to be able to fly a spaceship to a planet, and have it “snap into” the gravitational field and behave like a plane while close to the planet.)
Edit: the code posted here has been fixed – see below. I’ve removed the guts of this post so that no-one inadvertently uses the old, highly borked code.
When I start gameplay my object (currently a capsule) is slightly above the landscape and it falls to the ground and slightly through it, then suddenly corrects itself and is fine thereafter. Jumping does not cause this issue (so if I jump afterwards, there’s no problem.)
I got all this working quite nicely, and then refactored it into:
FauxGravityAttractor – gizmo that exerts pull on objects; it resolves the pull and “upvector” stuff internally, so it could (theoretically) easily support non-spherical gravity (e.g. ring worlds, or local mono-directional gravity as seen in Mario Galaxy).
FauxGravityBody – objects that fall towards attractors. The simple version right now is that each body is only attached to one attractor at a time.
FauxGravitySwitch – objects that reassign objects to new attractors when a specific event (e.g. collision) occurs. The idea is to allow a spaceship fly up out of one planet’s gravity-field … into zero g … and then enter another gravity-field.
Attach a dummy (ie. empty game object) to the object that you want the camera to follow (ie. the player).
Position the dummy so as to look at the player from the distance and angle desired when standing still.
Program the camera to interpolate to the dummy’s position, using a fractional interpolation (eg. 5% per interpolation) so as to get a nice bit of lag and easing.
I was thinking like that, but I hadn’t got that together in my head just yet.
you’re right!
child the null (dummy) to the player.
mouse orbit the camera to the null.
I could even child a target out in front of the player for the camera to look at, so that the camera is always looking toward what the player is facing.
I know you are talking about doing this with code. I should try to think that way too.
Here are my three gravity scripts – hopefully self-documenting
Basically, assign FauxGravityAttractor to each “planet” (or whatever) and assign FauxGravityBody to anything you want to move around on planets. I try to use the RigidBody properties where possible – so it respects mass, and makes objects with freezeRotation set self-righting. (So set freezeRotation to true for characters, but false for random objects.)
Note that I’ve got a “ground” layer set (10) for detecting whether an object is “grounded”.
Obviously for a more realistic simulation gravity would decrease with distance via the inverse square law, and for even more realism, everything would be attracted to everything else … but this works very nicely for game play (and it’s not like the built-in gravity is realistic in this way)
I just saw this article on Gamasutra and I thought that anyone checking this thread out would also be interested in it. It’s called " Games Demystified: Super Mario Galaxy" and it is about the physics in that game.
Also, even cooler, it includes the code they used for the prototype of the game!
I developed a game in Shockwave3D that does this after noodling with it for a couple of years. I used more or less the same approach, buttressed with some raycasting trickery.
In fact, I’m working on a Unity game right now that uses the same approach, only to much better effect, thanks to Unity’s being about a gazillion times better than Director Shockwave. I should have some demos ready to post soon.