align the rotation of a gameobject to the angles of two vector 2 points (or linecast)

im making a simple 2d game for practice purpose, but im having trouble making this

i have two vector2 points in space, and i put a line cast from a to b.

now i want to align a gameobject (a cube) to the rotation of the linecast (only the z axis), but i just dont find the way…

alt text

any can help me?

I know i’m late but i think you might be able to use math y=mx+b to isolate m(slope).
I came up with that, you only have to add transform.rotate or whatever you use to it.

var pointA : Transform;
var pointB : Transform;
var pAx : float;
var pBx : float;
var pAy : float;
var pBy : float;
var slope : float;

var angle : float;

function Start () 
{
	pAx = pointA.position.x;
	pBx = pointB.position.x;
	pAy = pointA.position.y;
	pBy = pointB.position.y;
	
	slope = (pBy - pAy)/(pBx - pAx);		//Simple y=mx+b, where b=0 and m= (yb - ya)/(xb - xa)
	
	angle = Mathf.Tan(slope);
	print(angle);
	
	
}

For a start see my answers to these two questions.

http://answers.unity3d.com/questions/406248/need-help-to-get-3d-rotation-from-2-points-in-spac.html

http://answers.unity3d.com/questions/444478/instantiating-platforms-between-two-vectors.html

Note the code in the first just rotates to match, the code in the second one rotates and positions halfway between the two positions. Depending on your geometry you may have to use something other than Vector3.up in the FromTo() calculation…Vector3.right for example.

But your diagram has an added complication in that you want the bottom to align with the line. Assuming this is not an arbitrary side, one way would be to place an empty game object on the bottom and make the visible object a child. The code would align the empty game object.