Wrong rotation while ladder climbing

Hello all,

I have implemented a ladder climbing script and at first having just one ladder in the scene, it seemed to work perfectly. Then I have placed other ladders in the scene with different rotations for testing purposes. And with those ladders it doesn’t work correctly. The player should be rotated always towards the ladder (look at it), but it is the case just on the ladder 1, not 2 and 3 - see picture below.

Within the script I have a helper method, which makes sure, that the player is always rotated towards the ladder.

// Script: Ladder climbing
angleRotated = Helper.MoveAndLookOnOneAxis(data.player, data.ladderPosition, 3);

The ladderPosition is calculated in another script:

// Script: Collision Detection
Ray collisionRay = new Ray(rayPosition, data.horizontalDirection);
if (Physics.Raycast(collisionRay, out RaycastHit hit, rayLenght, data.mixedLayer))
{
    data.collisionDetected = true;

    if (hit.transform.tag == "Ladder")
     {
         data.ladderDetected = true;
         data.ladderPosition = hit.transform.position;

And here is the implementation of the Helper.MoveAndLookOnOneAxis method:

// Script: Helper methods
public static float MoveAndLookOnOneAxis(GameObject player, Vector3 lookTarget, int lookingSpeed)
{
    Vector3 targetPoint = new Vector3(lookTarget.x, player.transform.position.y, lookTarget.z) - player.transform.position;
    Quaternion targetRotation = Quaternion.LookRotation(targetPoint, Vector3.up);
    player.transform.rotation = Quaternion.Slerp(player.transform.rotation, targetRotation, Time.deltaTime * lookingSpeed);
    float angleRotated = Quaternion.Angle(player.transform.rotation, targetRotation);
    return angleRotated;
}

And I know that the problem is within this (helper) script, because when I disable it, the player doesn’t rotate at all.

But I cannot figure out, what the problem is. I think that either somehow the wrong “LookTarget” is selected, or the look direction is taken somehow “globally” and not “locally”.

Any thoughts?

Thank you
Huxi

well , from what i see what you’re doing is not “taking the ladder’s rotation” , you’re just taking the “LookAt” rotation between the player and ladder.
In your first example , the “LookAt” happens to match the ladder’s rotation but that’s just a coincidence , imo just take the ladder’s rotation directly from its transform and just use that directly , some like this

public static float MoveAndLookOnOneAxis(GameObject player, GameObject lookTarget, int lookingSpeed)
{
    // Notice how i pass the lookTarget's GameObject , and not just the position
   // you can even just pass the rotation
    Quaternion oldPlayerRot = player.transform.rotation;
    Quaternion newPlayerRot  = Quaternion.Slerp(player.transform.rotation, lookTarget.transform.rotation, Time.deltaTime * lookingSpeed);
    float angleRotated = Quaternion.Angle(oldPlayerRot , newPlayerRot  );

    player.transfrom.rotation = newPlayerRot;
    return angleRotated;
}

Hello @PixelEyesDev,

thank you for your answer. This is it, the ladder rotation is now taken into account. One small problem: The player is rotated not forward, but backwards - so 180 degrees more then he should. I guess this is caused somewhere else in my code, as I don’t see any reason why this should be caused by the helper code.

That’s it. Thank you :slight_smile: