How to match rotation of Object to a face on another Object

Hello, I’m having some issues working out how to solve this problem:

140176-facefollow.png

My game is relatively simple at the moment, a cube travels along a track. However this track is slightly bent (which is necessary). It is cylindrical, so the player is able to move sideways around the track, with local gravity.

As the player moves forwards, they follow the black line, this is due to the slight bend. However what I’d like instead is for the player to generally follow the direction of the track as they move forward (i.e. the white line instead). I’ve realised that one solution to this would be simply if the player rotated on it’s local Y axis to face the same direction (+some offset rotation) as the underlying face that it is in contact with, which as I understand could use a raycast.

I’ve not had any luck getting this to work using something like the following:

   if (Physics.Raycast(ray, out hit))
    { 
            transform.rotation = Quaternion.LookRotation(new Vector3(hit.point.x, transform.position.y, hit.point.z));
    
    }

If anyone has any ideas or solutions (I’m new to Unity), please let me know, Thank you.

Have you tried using a Rigidbody? If you have not, what you can do is attach a Rigidbody component to the cube, make it Kinematic, and then adding a force that is in the direct you want.

void Update()
{
     Rigidbody rb;
     rb = theCube.GetComponent<Rigidbody>();
     
     rb.AddForce(float xForce, float yForce, float zForce * Time.deltaTime);

}

I hope this helps in some way, I am sorry if I didn’t understand your question properly.