I am not sure what you mean with "modify the X axis". If you intend to do transform.rotation.x = value
, then you are going to fail, because quaternions are not that simple. There is a reason why the documentation says "Don't modify this directly unless you know quaternions inside out.". You should only modify the individual fields of a Quaternion when you actually understand the math behind them. And very few developers actually understand it, so don't feel bad when your head starts to hurt. Just accept that quaternions are magic and get on with your life.
Or maybe you are thinking about rotating a Quaternion with transform.rotation *= Quaternion.Euler(x, 0.0f, 0.0f);
. Now you are on the right track. But when you want one object to rotate so that it faces a certain point, then you have to calculate the values for x
yourself. And if your knowledge of trigonometry isn't the freshest, you might have to think a few minutes about how to calculate that correctly.
That's why there is a convenient method just for this common use-case.
If you just want one object to rotate in a way that it faces a different game object, you use Quaternion.LookRotation(position);
. Now you might wonder why you need the additional "upwards" vector. Note that when you are looking from the perspective of the object, then there are three ways to rotate it:
You usually want to change only pitch and yaw and keep the roll the way it is. But sometimes in order to do that you need to tell the engine the difference between pitching, yawing and rolling. That's what the upward
-vector is for. Note that this is an optional argument. You can omit it. The engine will then assume you mean upwards in world-space. But sometimes you want to use a different upward-axis. The upward
parameter allows you to do that if you want to.
For a possible use-case, consider a space ship with turrets attached on the top and on the sides. You want all those turrets to be able to track enemy fighters in a hemisphere oriented relative to their mounting-point. The best way to do that is obviously by using Quaternion.LookRotation(enemy.transform.position - turret.transform.position)
. But you still want all the guns to stay level relative to the hull of the space ship and not rotate around the barrel-axis. This is additionally complicated by the fact that the ship itself is able to rotate. So you need to tell LookRotation
which way is up for that specific turret.