'Terrain normal has wrong direction

I'm trying to rotate my player along surface. Like transform.up = ground.normal

So now I'm debugging GetInterpolatedNormal() and it always has the same direction. Could you help me with that?

Code reference: https://answers.unity.com/questions/868898/make-gameobject-always-vertical-to-terrain.html

 private Vector3 SampleNormal()
    {
        var terrain = Terrain.activeTerrain;
        var terrainLocalPos = transform.position - terrain.transform.position;
        var normalizedPos = new Vector2(
            Mathf.InverseLerp(0f, terrain.terrainData.size.x, terrainLocalPos.x),
            Mathf.InverseLerp(0f, terrain.terrainData.size.z, terrainLocalPos.z)
        );
        var terrainNormal = terrain.terrainData.GetInterpolatedNormal(normalizedPos.x, normalizedPos.y);
        Debug.DrawRay(transform.position, terrainNormal, Color.red, 5f);

        return terrainNormal;
    }

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source