Vuforia, C# rotating an object to point another object fixing Y Axis
Hi everyone, i'm new in Vuforia, i'm approaching it for about one months, but i made a lot of project examples, so i know something :). Now i'm trying to build an other application. The scenario is this:
Two image targets, let's call them T and J;
Two objects, let's call them X and Y;
When T is recognized X appears on the scene and its position is fixed, when J is recognized Y appears and it moves along z axis. Image Targets can recognized together, X and Y can appear at the same time. What i want is that X point Y, always! Also if Y is moving, but i want rotate X along one and only one axis, in particular its y axis. This is my scene: https://imgur.com/a/G5yAy I've tried a lot of things:
transform.LookAt (target, target.up);
But I don't like the result, because i want a natural rotation with a certain speed. I tried also this:
float z = Vector3.Distance(transform.position, target.position);
float x = transform.position.x - target.position.x;
float y = transform.position.y - target.position.y;
float angle = Mathf.Acos((Mathf.Pow(y, 2.0f) + Mathf.Pow(z, 2.0f) - Mathf.Pow(x, 2.0f) / 2 * y * z));
angle *= Mathf.Rad2Deg;
if (x > 0)
angle = -angle;
transform.Rotate(Vector3.forward, angle);
But Mathf.Acos returns always NaN and i know the answer, it is the formula passed as parameter, this formula returns a number out of [-1, 1]. So, I've tried to normalize the result of the formula but normalisation returns always 1 because formula returns always a number bigger than 1. At the end i've tried this solution:
float speed = 1f * Time.deltaTime;
Vector3 dif = secondaryShip.transform.position - transform.position;
dif.y = 0;
Quaternion lookAngle = Quaternion.LookRotation(dif, transform.up);
this.transform.rotation = Quaternion.Slerp (transform.rotation,
Quaternion.LookRotation(dif, transform.up),
speed);
This solution is really near to what i want, but there is a strange problem, X rotates only for 180° and i don't know why!? The result is this: https://imgur.com/a/0C44h X doesn't seem to point the Y. After all this attempts i want to ask for your help. In particular, X has to rotates facing Y, following it when it moves and don't forget that X has to rotate only around its y axis. Please Help me! Thanks in advance :D