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