It’s 3 am, I’ll try to be clear… transform.position is the vector start point, let’s say A(x,y,z). B(a,b,c) is the end of the vector. Consider this vector as a line in space, direction is irrelevant. Now, how should I rotate the object I have so its x axis would align with the vector AB? I mean, through code, using c#. If I know A and B, how would I calculate the transform.rotate for the object?
Thank you in advance.
Vector3 v = B - A;
Quaternion q = Quaternion.FromToRotation(transform.right, v);
transform.rotation = q * transform.rotation;
Or simpler:
Vector3 v = B - A;
transform.rotation = Quaternion.FromToRotation(Vector3.right, v);
transform.right = B-A;
Thanks, this works perfect, is there a way to add a Linear Interpolation?
Vector3 v = go.transform.position - transform.position;
Quaternion q = Quaternion.FromToRotation(-transform.up, v);
// Changed next line for the last one
//transform.rotation = q * transform.rotation;
transform.rotation = Quaternion.Lerp(transform.rotation, q, Time.deltaTime * alignSpeed);
And with this change weird stuff happens