How to rotate Cube to match slope

I have been having a problem trying to rotate my cube to match a little hill I made. For some reason the cube is only straight while on a slope. The picture below is what I am trying to achieve (I did not make it).
The guy that made that post also replied to it with an answer to his solution. His solution was the following code

 upright = Vector3.Cross(transform.right, -(fHit.point - bHit.point).normalized)
transform.rotation = Quaternion.LookRotation(Vector3.Cross(transform.right, upright));

His solution was to make 2 raycasts that look down and use the following above code to rotate your cube. Although I have a problem with it.
I don’t want my cube to clip the ground whenever it goes down a slope. Is there anyway that can be fixed. Also will I have to make 6 other raycasts if I want to achieve the same effect on all the other sides (i.e. if I jumped up and hit a slope would the cube rotate to match the slope it hit above it, or if the cube was falling would and hit a steep slope and slid down it on it’s side would it match it). I was planning on making my cube rotate everytime you jump so it doesn’t look like just upward force is applied. Would that screw up this script. Thanks for any help if provided.

1 Like

Are you sure you don’t want to use Unity’s built-in physics for this?

What do you mean by that

Physics. Like…rigidbodies, colliders, that stuff? Making objects behave in basic physically-realistic ways is a common need and Unity has this whole edifice specifically designed to do that.

1 Like