EDIT: The solution to title question is this:
Quaternion.LookRotation(Vector3 desiredDirectionOfRotation, Vector3 desiredDirectionOfUpUsuallyVector3.up);
Original (more off-topic) post:
I’m making an RTS. When I have multiple units selected and click and drag I want them to form into a row between the locations of the mouse on buttondown and buttonup and then rotate 90 degrees left from that line.
What I have a problem with is that last part.
What I tried:
Get a cross of the two points and pass it to the unit function;
Vector3 A = rightClickStartHit.point;
Vector3 B = rightClickEndHit.point;
//Vector3 AB = rightClickEndHit.point - rightClickStartHit.point;
Vector3 turn = Vector3.Cross(Vector3.up, A - B);
foreach (GameObject go in listOfSelected)
{
Vector3 C = Vector3.Lerp(A, B, j);
go.GetComponent<Unit>().Move(C, turn);
j += i;
}
Then use Quaternion.Slerp to rotate towards the euler of turn:
public void Move(Vector3 targetPos, Vector3 targetRot)
{
isTurning = true;
//targetRot = targetRot.normalized;
lookRot = Quaternion.Euler(targetRot); //this gives 0?
lookRotV3 = targetRot;
Debug.Log("lookRot = " + lookRot);
agent.destination = targetPos;
}
// in Update
if (isTurning && agent.remainingDistance <= agent.stoppingDistance)
{
Debug.Log("current = " + transform.rotation + " desired = " + lookRot);
//lookRot = Quaternion.FromToRotation(transform.forward, lookRotV3);
transform.rotation = Quaternion.Slerp(transform.rotation, lookRot, 1 * Time.deltaTime);
//transform.rotation.eulerAngles
//transform.Rotate(lookRot * 1 * Time.deltaTime);
if (Quaternion.Angle(lookRot, transform.rotation) < 16)
//this only stops if set to < 20 but that's ugly
{
Debug.Log("Stopped turning");
isTurning = false;
}
}
As you can see I tried a bunch of things but I don’t understand quaternions and vectors at all. The resulting target rotation always ends up being a 0.0.0
I’ll add the object is in a plane and I only want them to rotate around the y axis.
Also additional question: How to rotate an object over time, but consistently, not like Slerp?
PS: All help highly appreciated, sorry for formatting.