Unity3D Camera Movement Revisited

>>Get the revised camera movement script with inertia from Github Gists<<

Some several months ago, I made a much visited tutorial about rotating, panning, and zooming a camera in Unity3d. I was asked if it was possible to make the camera movement more smooth. While I thought the movement was already smooth, no one can ignore the fact that if you let go of mouse when moving the camera, it will suddenly stop.

A more desired behavior is for the camera to smoothly stop after letting go of the mouse. In order for this to work, the camera needs to have some inertia, thus physics properties.

In the link above with the revised camera script, I implemented a version of camera movement that will stop smoothly. You can even control this smoothness from the pubic variables turnDrag, panDrag, and zoomDrag.

Internally, a RigidBody component would be required for this to work, but I made the script take care of that, so that you don’t have to. In other words, if you’re using this script on a camera, do NOT add a RigidBody component because the script will do that automatically.

Continue reading

Advertisement

Planetary rotations and revolutions in Unity

———————————————————–

Work on Caelium is finally under way, but there will probably be only gradual progress. I implemented a system where celestial bodies can orbit around other celestial bodies and also a system where the bodies rotate on their axis.

To do this, two rotations were required: axial (around its axis) and orbital (around another body).  In Unity, when I tried to apply both types of rotations on a body (as a single GameObject), only the orbital rotation seemed to work.

To fix this problem, I had a simple GameObject hierarchy. You start with a CelestialAnchor, which is basically a point in space that orbits around another CelestialAnchor. It’s responsible for the position of the planet/star/moon and its orbital rotation.

Each CelestialAnchor holds a CelestialBody, which contains the 3D model of the planet/star/moon and does the axial rotation.

In this hierarchy, the planets have an orbital and axial rotation applied, as seen in the video. The speed of the rotations and the distances between the bodies can be tweaked in the editor.

Rotating, Panning, and Zooming a Camera in Unity

————————————————

Link to code on bottom of post. ↓

————————————————

In Caelium, one of the features I wanted was simple camera movement system that could rotate the camera, pan it on the plane defined by its forward direction, and zoom. In the video, I demonstrate the movement of a camera in Unity.

To rotate the camera, you left click the mouse and move around. To pan the camera, you right-click the mouse and move around. To zoom, you middle click the mouse and move up or down.

By no means is it perfect, but what I have now is pretty good, as you can see in the video. I’ll explain how the movements work.

Continue reading