I'm making a game in which the player controlls a ball. (Wow, such originality^^) It's a 3rd person game so the camera is slightly above and behind it. The ball is being controlled via WASD. Now the tricky part is, I want this to work totally independent from the worldaxis so or example even upside down. So in my PlayerControllerScript I have an array of three vectors which represent the new X,Y and Z axis of my ball. The X axis always points in the direction the sphere is moving. The Y axis is calculated by the forces influencing the body. So if the gforces on the body are strong enough, for example while rolling along a craterslope, my Y axis is going to be horizontal. The Z axis is always orthogonal to the firs two.

So much for the explanation. So the camera is always being offset according to those new axis' and then pointed in direction of the gameobject using this line:

T.rotation = Quaternion.LookRotation (TargetTrans.position - T.position);

T being the Camera's transform. I don't really understand this line but it works. My problem is, that this makes the camera always parallel to the horizon, but I want the camera to tilt with the objects movement. So when I'm rolling in circles on a craterslope it should not be parallel to the horizon but to the slope. I tried to Rotate the Transform of the camera around the calculated X axis like so:

T.Rotate(PlayerScript.Orientation[0] , Vector3.Angle(Vector3.ProjectOnPlane( Physics.gravity,PlayerScript.Orientation[0]) ,OtherScript.Orientation[1]) );

Notice that I'm taking the angle between my calculated Y axis and the gravity beeing projected onto the YZ Plane of y calculated orientation. Like this I avoid unwanted tilting, for example when moving straight up the crater slope away from the center. However this line doesn't change anything visually. What's wrong? It's not because the rotation is changed right afterwards with the first command I gave, because the Rotate command comes after it.

I'm starting to realize rotation is a lot more complicated than I thought and eventhough this has to be a very common problem in for all peple who develop games with planes, I couldn't find anything. :(

Help me please... Thanks in advance.

I also realize how hard this even is to explain so let me know if something is not clear.


1 Answer 1


You're going to laugh at how easy this is. ;)

You know this line?

T.rotation = Quaternion.LookRotation (TargetTrans.position - T.position);

Add your ball's vertical axis as a second parameter:

T.rotation = Quaternion.LookRotation (
                TargetTrans.position - T.position, 

(You might need to negate that second parameter if your vertical axis points down toward the ball's ground plane, rather than up & away)

How this works:

  1. TargetTrans.position - T.position is a vector pointing from your camera to the object.

  2. Quaternion.LookRotation constructs a rotation that rotates the forward axis to point in the direction of that first argument. So that gets the camera looking in the direction toward the ball.

  3. The second direction is optional, and it's used to give a hint for where the local "up" vector of the rotating object should point. By default/if omitted, it uses Vector3.up = (0, 1, 0), which is what keeps the camera oriented level vs the horizon. But if you provide your own up vector, it will try to roll the object around the look direction to keep its local up aligned with the second direction you provide.

One last little note is that, for working in Unity, you'll likely have an easier time if you adopt Unity's coordinate system conventions. There Z represents "forward" (see eg. Vector3.forward) and X represents "right", so it's flipped relative to the axis naming you described for your ball. Keeping one consistent coordinate convention will help avoid mistakes and bugs.

  • \$\begingroup\$ Thanks a lot! :D I can't believe it was this easy... The X/Z axis issue explains why it flips 90° at the start. So you helped me twice. ^^ \$\endgroup\$
    – Konny
    Jul 14, 2017 at 16:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .