As far as I understood the complex relations between a directional Vector3 like transform.up, a Vector3 storing euler-angles and those devilish quaternions, setting only one directional Vector only locks 2 rotation-axes.
E.g. if you set the up-vector of yourself to look skywards, you have to stand all the time on your feet, so your head points to the sky (x-axis and z-axis locked). However, you could rotate around your own y-axis as your head would still point to the sky.
That’s where my problem kicks in:
I’ve coded a script that does movement on an uneven terrain or planet, parallel to the ground.
For achieving the desired effect, I’ve did these things:
My character is moved forward (transform.position += transform.forward * speed * time.deltaTime)
A ray is casted from my character downwards (-transform.up).
The player is moved to the point where the ray hit the planet (transform.position = hit.point)
The player’s up vector is set to the normal of the point where the ray hit the planet (transform.up = hit.normal)
It works so far, however I simply can’t rotate the player on the y-axis.
I’ve got a sample of the effect you may be looking for: http://forum.unity3d.com/threads/72665-Planetary-Gravity
Look for Ntero near the bottom. It’s VERY basic and ugly(a few hours of testing and tinkering), but it shows how to handle directional gravity, it’s on a Sphere but the code works easily as well on a tetrahedron or a cube.
Using the Cross Product or Lookat allows you to get to axis, and therefore specify a full rotation, whereas setting up will just specify a plane and can potentially get all wonky as an arbitrary rotation is chosen.
Yay, I’ve finally found a solution on my own. @Ntero: Your code is not exactly what I need unfortunately.
Here’s my solution:
Setting the up-vector to the normal of the poly my object stands on locks all axes, this seems inevitable.
Hence, I employ a parent object. The parent object gets moved in the direction of the child-objects forward vector and the parent objects up vector is set to the raycast hit normal.
The rotation, though, is applied to the child object.