Quaternion rotation between 2 points(unsolved)

Hey everybody, Lately I’ve been using Unity a lot for school related purposes, and I’m now the lead programmer for our final game project which we’re doing in Unity. With that in mind, I figure it would be a good idea to join this forum and get friendly with you all :P.

Anyway, My first question, is how do I create a quaternion rotation that represents the rotation between 2 points? I don’t know if that’s the proper way to word the question, so I’ll explain what I’m trying to do.

I have 2 points in 2D space, that is, the X value is always the same, and the Y and Z values change depending on their location on a plane.

I’ve also got a 3D plane that I want to stretch between the 2 points, so I need to rotate the plane around the YZ axis to the angle between my 2 points. This would be fine If Unity worked in Euler angles, but Unity refuses to conform to my primitive ways and demands I learn to use quaternions.

Here’s what I’ve got so far, this code refuses to offer any relevant results:

var rotangle = Vector3.Angle( 
Vector3(projectedPoints[1].y,projectedPoints[1].z, 0), 
Vector3(projectedPoints[2].y,projectedPoints[2].z, 0)
);

//Set the plane position between the 2 points
currentplane.transform.position =((projectedPoints[1] + projectedPoints[2])/2);
currentplane.transform.rotation = Quaternion.AngleAxis(rotangle, Vector3.right );

I can’t for the life of me figure out what I’m doing wrong :(.

EDIT: It appears the rotation between the 2 points is incorrect, But I still can’t figure out how to get the correct rotation.

Try

transform.eulerAngles

:slight_smile:

Or

transform.rotation = Quaternion.Euler(anglevector);

which is basically the same as above and rotates in this order: z → x → y

Unfortunately both of those methods are giving me the same result as before.

Is it possible

var rotangle = Vector3.Angle( Vector3(projectedPoints[1].y,projectedPoints[1].z, 0 ), Vector3(projectedPoints[2].y,projectedPoints[2].z, 0 ) );

is not actually giving me a correct rotation between 2 points?

EDIT: I did a couple of tests, and the rotation is indeed out to lunch. If I draw a line between point 1 and point 2, the points are connected with the line properly. But, if I draw a line from point 1 pointing in the direction that is supposed to be the direction between the 2 points, it’s drawn jutting out towards some random location that has little to do with the actual angle between the points. I also tried using

var angleb = Mathf.Atan2( projectedPoints[1].z-projectedPoints[2].z, projectedPoints[1].y-projectedPoints[2].y );

But that ended up getting me similarly wrong results. Any ideas what’s going on here?

i’m not sure i understand your problem, but check if this is what you want:

(to make it work, you need 2 cube, for example, and one plane, the plane has the script, trans1 and trans2 is a link to the transform of the 2 cubes.)

private var projectedPoints : Vector3[];
var trans1:Transform;
var trans2:Transform;

function Update () {
	projectedPoints=[trans1.transform.position,trans2.transform.position];
	var lookAt:Vector3=(Vector3(0,projectedPoints[0].y,projectedPoints[0].z)-
	Vector3(0,projectedPoints[1].y,projectedPoints[1].z)).normalized;
	transform.position =(projectedPoints[0]+(projectedPoints[1] - projectedPoints[0])/2);
	transform.LookAt(transform.position+lookAt,Vector3.up);
}

i think the problem you have is:

Vector3.Angle(position,position);
means, for example, if you have two spots in a coordinate system: (5,0) and (0,2), Vector3.Angle((5,0),(0,2)) will tell you the angle between the lines (0,0)-(5,0) and (0,0)-(0,2). it is the same as Vector3.Angle((1,0),(0,1));

if you use Vector3.Angle((56,99),(55,98 )), then these lines are very close to eachother, the angle will be small.

what you need is a vector that points from (56,99) to (55,98 ), and that is (-1,-1) i think.

Sorry if i misunderstood something.

Yeah, your original problem is that you’re not using Vector3.Angle correctly. It takes two directional vectors, and returns the angle of difference between them. Which you actually probably don’t want. It looks like you just want a rotation that is the direction from point a to point b. For that you want Quaternion.LookRotation(look in script reference for parameter info). For the ‘view direction’, you supply the direction you have, which is your end position minus your start position. Vector3(projectedPoints[1].y,projectedPoints[1].z, 0) - Vector3(projectedPoints[2].y,projectedPoints[2].z, 0)
the second parameter is the direction you want the plane’s suface to face. Which is probably just Vector3.right

so something like

currentplane.transform.rotation = Quaternion.LookRotation(Vector3(projectedPoints[1].y,projectedPoints[1].z, 0) - Vector3(projectedPoints[2].y,projectedPoints[2].z, 0), Vector3.right);

Perfect, thanks cerebrate that did the trick. Now that I see this working I can confirm that my method isn’t going to work XD.

Back to the drawing board I guess.