- Home /

# Direction to Matrix

I'm having a hard time converting directions to rotations. How can I orient a transform to be aligned by my directions?

```
transform.up = myUp;
transform.right = myRight;
transform.forward = myForward;
```

I risk overriding one of the assignments by the following, since direction only determines 2 axis, the 3. is arbitrary.

### People who like this

**Answer** by MUG806
·
Feb 02 at 03:27 PM

Best way is via a quaternion:

```
transform.rotation = Quaternion.LookRotation(myforward,myUp);
```

I haven't tested, but does that take care of the right component?

Tested it, it seems to be working in my case, but could someone confirm, it's not a coincidense and the look rotation actually has only 1 possible result?

A usual matrix (that's under the hood of the Transform component) always has perpendicular axis. So two axis are enough to completely specify a coordinate space since the third is just the cross product between the first two. If you actually want to have a matrix that does allow for skewing, you can setup a matrix on your own. However, you can not assign it to a Transform as a transform's local matrix is always a perfectly orthogonal coordinate system. Due to non-uniform parent scaling and rotating you can get a skewing effect of a children, however the local transform is never.

Assigning forward, up or right would individually calculate a new orientation where you can't control the other axis. So in most cases that's not really helpful. LookRotation does actually construct a complete rotation. The look axis will always be exact, the given up axis is just used as a hint. So if the up axis is not perpendicular to the given look axis, it's essentially projected onto the perpendicular plane.

If you're looking for some general information on matrices, I wrote a crash course over here. For more information on matrices in general, I would recommend to watch the essence of linear algrebra series by 3b1b. It takes some time and if you're new to linear algebra you may need to rewatch some of them, but it may help to get a better understanding about vectors, matrices and transformations.

There is only one possible right direction for any forward and up direction.

### Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. **Please note, Unity Answers is now in read-only so we can prepare for the final data migration.**

For more information and updates, please read our full announcement thread in the Unity Forum.

### Follow this Question

### Related Questions

Simultaneously translating and rotating 2D sprite? 1 Answer

Rotating object on one axis using Input.GetAxis 2 Answers

Why does this code not work? 1 Answer

Rotate Vector3 array around a point 1 Answer

How to get a proper ramming effect? 1 Answer