0

I've gone through Unity's documentation for Quaternion.LookRotation, but I didn't get the full understanding of how it works.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    public Transform target;

    void Update()
    {
        Vector3 relativePos = target.position - transform.position;

        // the second argument, upwards, defaults to Vector3.up
        Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up);
        transform.rotation = rotation;
    }
}
2
  • 1
    To be able to better answer your question please edit your post to include what specific part of the concept you are struggling with or at least what your current understanding of the matter is. I know it is hard to ask specific questions without a complete understanding of the topic but putting effort into it will bring you 75% of the way to the answer and maybe we can help from there.
    – ymindstorm
    Feb 12, 2021 at 10:07
  • 1
    Well it returns a rotation that makes your object look into a certain direction. No big magic ;) What exactly do you lot understand about it?
    – derHugo
    Feb 12, 2021 at 15:14

2 Answers 2

3

A way to visualize a rotation is with three perpendicular axis-ex. A quaternion is a more compact representation, but you can still view it as having three axis-es.

LookRotation will align one of the rotation axes to the given direction, but with only one direction there is one degree of freedom left, the other two rotation axes.

That is what the 'up' vector is for, it locks in one of the other axes and forces it to be perpendicular to both the direction and up-vector. The third rotation axis is always perpendicular to both, so we have three perpendicular axes, i.e. a complete rotation.

You can do something similar yourself with a cross product, since that produces a perpendicular vector to two others. Pseudocode:

var xDir = direction;
var zDir= xDir.CrossProduct(upVector)
var yDir = zDir.CrossProduct(xDir)
var matrix = CreateARotationMatrixFromAxises(xDir, yDir, zDir)
var quaternion = CreateQuaternionFromRotationMatrix(matrix)

Note that the direction and up-vector cannot be parallel, or you will get some kind of error.

-5

There is absolutely no reason for beginners and hobbyist programmers to touch Quaternions. Unity should remove it from the documentation.

What you want is simply the LookAt command. Fortunately, it's incredibly easy to use.

Say you have a tree that you want your character to look at, it's this simple:

transform.LookAt(tree);
4
  • 3
    Not sure why people are so afraid of Quaternion .. it is literally a ready to use "library" that does all the complex maths behind it for you ^^ It would really get complex if you would need to calculate that stuff on your own .. and well, yes, Transform.LookAt basically does the same for now ... But now try to interpolate that in order to have a smooth rotation ;)
    – derHugo
    Feb 12, 2021 at 15:13
  • 2
    We use quats to avoid gimble lock. I'm not sure why you say no one should use them. A lot of programmers can't derive a projection matrix on their own. Do you think they shouldn't use perspective?
    – 3Dave
    Feb 12, 2021 at 15:18
  • 2
    @Fattie well maybe .. or you are underestimating the common programmer a lot ;) Of course nobody should touch Quaternions in a sense of manually calculate the individual components ... That's what the type Quaternion already takes all care of for you .. the se way you could just say don't touch Vector3 .. better just leave your objects right where they are ^^ OP asked what Quaternion.LookRotation is good for and how it works .. you answered none of that actually ;) Unity should remove it from the doco is probably the most short sighted statement I came across today ^^
    – derHugo
    Feb 12, 2021 at 16:05
  • 2
    If you want to learn programming apps in 3D and Unity .. well then there won't be a away around also learning some basic API reading and .. yes .. maths! At some point you'll have to deal with it and not avoid it where possible .. the sooner you start the better
    – derHugo
    Feb 12, 2021 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.