Setting a Vector3 normal as the axis of an object

Hey, I’m working on this little game mechanic where the player character gravitates towards surfaces that are set to attract them. I already have the detection working, I can find the direction toward an object’s surface and the normal that is needed to align the character to the surface correctly.

However, I can’t wrap my head around the rotations and I just can’t get it working.

fallingDirection = closestPlanet.GetComponent<Collider>().ClosestPointOnBounds(transform.position) - transform.position;

fallingDirection.Normalize();

transform.rotation = Quaternion.LookRotation(-fallingDirection);

Currently it does rotate the character towards the ground, but the axis are messed up. If anyone knows how to fix this I’d love to hear it.

LookRotation can take 2 parameters. The first one should be the vector representing the player’s forward direction in world space, the second should be what vector should be considered “up” for the rotation, again in world space.

By default (i.e. when not supplied) the second parameter is Vector3.up and since you are trying to orient your object to rest on surfaces that have a different “up”, that obviously isn’t going to work. You probably want to pass your object’s transform.forward as the first parameter and -fallingDirection as the second so that your object continues to “face” the same direction while pivoting so that it is “upright” for the relevant surface.