Get Y Rotation Between Two Vector3 Points

Basic Question

I am struggling to get the Y rotation between two Vector3 points. I have two points and I want to position a cube so it is rotated in alignment with the two points on the Y axis.

What I am currently doing is this:

var essenceRotation = Quaternion.FromToRotation(Vector3.up, pointA - pointB);
cube.transform.rotation = Quaternion.Euler(0, essenceRotation.eulerAngles.y, 0);

Additional Details

I am using the Hololens and I want to have the game object (just using a basic cube for now) to be aligned with a wall. Normaly I would just do a raycast and get the rotation from the raycast hit. However this wall is a shiny surface, and that messes with the Hololens sensors and makes it think the wall surface has all these weird angles. So when I take this normal approach I get back a weird angle.

So I figured that I could just do two raycast’s and get two points a short distance apart, and then get the rotation on the Y axis between these two points, and this should give me a more accurate result.

I tried the solution I shared above, but it only works if you are squarely facing the wall. If you are facing the wall at an angle then the rotation angle I get is wrong.

Any help would be highly appreciated. Thank you.

Calculate Direction Vector, bamm you done

Try this:

Vector3 target;
Vector3 origin;

Vector3 dir = target - origin;

Quaternion rotation = Quaternion.Euler (0, dir.y, 0);

transform.rotation = roration;