I have a script which aligns the player with a given collider (typically a box collider over a ladder). So far, I’m able to make them perpendicular to the box collider and set the an appropriate distance … what I’m curious about is how do I “center” the player to the ladders box collider? Code below (Player = player transform w/collider, Target = ladder transform w/box collider):
// rotate the player to look straight on to hit surface and adjust their distance to a predictable amount
Player.rotation = Quaternion.LookRotation( -Hit.normal );
Player.position = Hit.point - Player.forward.normalized * ( Player.collider.bounds.extents.z + paddedSpaceFromSurface );
Ya maybe I’m over (under :)?) thinking this but it just seemed like you want to set x,z = ladder x,z (keep players existing y). That centers us on ladder at players current y coordinate. But since you could be rotated, I’m using the new forward vector (which is facing the ladder now) and moving back on that.
So is the last piece really just finding the x of the object and matching the player to it? I’ll try inserting a step to do that before backing the player off from the ladder and see how it goes.
*Edit: alright, doesn’t work - problem is x is a world space metric that is not relative to the rotation of a given object, so I need to shift the player left or right - probably using transform.right and adjusting to center it on the collider ahead.
// adjust player positioning on this surface so they are squared to it and a predictable distance from it
Player.rotation = Target.rotation;
Vector3 yAdjustedHit = new Vector3( Hit.point.x, Player.transform.position.y, Hit.point.z );
Player.position = yAdjustedHit - Player.forward.normalized * ( Player.collider.bounds.extents.z + paddedSpaceFromSurface );
// does not work:
Player.position = new Vector3( Target.collider.bounds.center.x, Player.position.y, Player.position.z );