Let’s Make a 4X Space Game with Unity – Part 9 Camera Control 3
Hi Guys and welcome to part 9, the final part of the camera control section! In the last part we clamped our panning and added zooming. In this part we will:
- Add rotation to our zooming.
- Fix the problem of varying pan speed with zoom level.
- Reset the camera when we switch views
1. Adding Rotation to Zooming
In the CameraController script we have already added our maximum and minimum angles for our zoom. We use Mathf.Lerp like we did with the zoom float to create a new float called zoomAngle . We then use this value to set our Rotation Object’s X rotation.
Press play and notice the camera now rotates when we zoom in and out! Change the values in the inspector and find the angle and distance combination you like.
2. Fixing the Panning Issue
This is an easy fix really. We need to adjust the panSpeed based on our zoomLevel so that it is its true value when it is fully zoomed in and it scales as we zoom out.
In the ChangePosition method declare a float called movementFactor. Set it equal to Mathf.Lerp between minZoom and maxZoom by zoomLevel. Then in the translation part add movementFactor as one of the multiples.
If we press play now the panning issue is fixed but the camera now moves way too fast! Change the pan speed to 5 in both the inspector and where we declare it in our code. This returns the speed to a much more sensible rate.
3. Resetting the Camera when Switching Views
You may have noticed if we pan the camera and then click on a solar system we sometimes cant see it because the camera is not centred. To fix this all we need to do is create an instance of the CameraController script (like we did with our Galaxy and SolarSystem scripts) and call ResetCamera() in the CreateSolarSystem method (in the SolarSystem script).
But now when we return to the galaxy view the camera will be wherever it was in our solar system. We should return to where the star we clicked on was. In our SolarSystem script declare a Vector3 called starPosition. Make it public with get and set. Then in Update just after we get the Star from the GameObject set it equal to the GameObject’s position.
Now in our CameraController script add a simple method, which is public and void, which takes a Vector3 and sets the Pan Object’s position to that Vector3. Call it MoveTo.
Call this method in the DestroySolarSystem method after the solar system has been destroyed. Pass it starPosition.
Press play and notice the camera seamlessly moves to (0,0,0) when viewing a new solar system and is above the star we clicked on when returning to the galaxy view!
This concludes this very short part. It also concludes our work on the camera for now! Next time we will improve the look of our solar systems by making it easy to see the planets orbits.
Credit: The camera control tutorials have been based on Catlike Coding’s hex map camera tutorial. I have used this system in several games in the past and have found it to work well. I have tweaked the code a bit and made it suitable for our purpose. Catlike Coding’s tutorials are great, especially if you are new to Unity and coding. His tutorials can be found here: http://catlikecoding.com/unity/tutorials/
scripts_for_part_9 (zip file)