This site uses strictly necessary cookies. More Information

X- Home /

# Get rotation from a vector direction.

**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.

**Answer** by Bunny83
·
Jul 21, 2017 at 11:23 AM

The success of this line:

```
Vector3 turn = Vector3.Cross(Vector3.up, A - B);
```

highly depends on which plane you use for your RTS game. This assumes that A and B are both inside the X - Z - plane. If you use the X - Y - plane (the usual 2d GUI plane) the cross product will result either in "Vector3.forward" or "-Vector3.forward"

In addition you should make sure that "AB" is actually a non zero vector. Or in other words: make sure your two hit points are not the same.

Thanks for your answer!

A and B are raycast on terrain in the X and Z plane. What I want to do is get an A -> B vector, rotate it 90 degrees left and use that to rotate the units - like in Total War games. But I'm failing to understand this.

Will Vector3.Cross(Vector3.up, A - B); get that A -> B vector turned by 90 degrees left?

This code:

```
Debug.Log("A = " + A + " B = " + B);
Vector3 AB = B - A;
Vector3 T = Quaternion.Euler(0, 90, 0) * AB;
Debug.Log("T = " + T + " AB = " + AB);
Vector3 turn = Vector3.Cross(Vector3.up, A - B);
Debug.Log("Turn = " + turn);
```

Prints these values: A = (-18.3, 0.0, 16.2) B = (3.2, 0.0, 18.7) T = (2.6, 0.0, -21,5) AB = (21.5, 0.0, 2.6) Turn = (-2.6, 0.0, 21.5)

So which one will be my +90 degrees? T or Turn? Thanks again for your help!

Well your two examples are very confusing since you use the vector AB for the quaternion rotation but the vector BA for the cross product. First have a look at your quaternion rotation:

You take the direction AB and rotate it 90° to the right since the quaternion rotation around Y is clockwise when you look at it from the top. It's counter clockwise when you look along the Y axis (from the bottom up). So T will be a vector that is 90° to the right.

Your cross product on the other hand will also rotate the vector 90° to the right but since you use the opposite vector (BA) you actually rotate the vector 90° to the left. That's why "T" is the inverse of "Turn":

```
T = -Turn
```

$$anonymous$$eep in mind that Unity uses a left-hand system so the cross product uses the left-.hand rule ins$$anonymous$$d of the right hand rule.

*edit*

To be more precise:

```
Vector3 AB = B - A;
Vector3 T = Quaternion.Euler(0, 90, 0) * AB;
Vector3 turn = Vector3.Cross(Vector3.up, AB);
```

In those two cases both "T" and "turn" should contain the same direction (roughly, keep in mind floating point numbers!).

ps: In a 2d environment the cross product can be extremely simplified since you use (0,1,0) as first vector. That basically leaves you with:

```
Turn90Right = new Vector3( AB.z, 0, -AB.x);
Turn90Left = new Vector3(-AB.z, 0, AB.x);
```

Yes, I inverted AB because I realized it was originally the opposite of what I intended to use it for, which was the correct result of the Cross after all.

Thanks a lot for taking your time to explain this to me. Can you push me in the right direction to figure out how to turn the direction of the calculated vector into a rotation I can set as target for Quaternion.SLerp or equivalent method of rotating over-time? And maybe suggest what would be the best method of rotating that avoids the "never quite reaches 1" problem of SLerp?

Again thank you so much for your time!

EDIT: I believe Quaternion.LookRotation is what I'm looking for. Now to find something better than SLerp!

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.