This is an extremely useful thing to know, fundamental even to making games that are even slightly more complex than the ones that we have made on the course so far.
In the video "Control Camera to Track Ball" we take a look at the basics of this, tying the camera's position to the ball's position on the lane.
Up to this point in Bowlmaster’s development the camera stays still as the ball moves down the lane towards the pins.
This makes seeing the glory of a strike, or the ignominy of a late gutter ball, a little difficult to see.
The solution is to move the camera along with the ball… at least, to a point!
This is achieved by adding a script to the camera and adding an instruction telling the camera’s transform position to remain a constant distance from the ball’s in the script’s Update function.
To do this, first create a script and attach it to the Main Camera. In the video we call it CameraController.
Within the script we need to link the camera to the ball. We do this by linking the ball to the CameraCaller script by declaring a public ball and ready to be dragged in through the Unity interface:
Once you have set up the link to the ball you need to determine the offset from the ball that the camera should adhere to in the Start() method.
Ben goes into some detail about this in the video, and even if you are familiar with the logic its worth going over again. Here we'll just input the result. The lines highlighted in Red are the ones that you need to add!
As you can see it is a hugely useful tool for your kit and very simple to extend or modify for use in other contexts.
Better yet get yourself on the Complete Unity Developer course as soon as you can and start learning from the experts!
Hi, my name is Tom, I'm a student on the Complete Unity Developer course and thanks to Ben and Brice I've learnt an incredible amount in a really short time.
Just a few short weeks ago I was tentatively printing out lines to console and now I am confident enough to be starting out on a project of my own!
Check out my blog about my first solo project Food Cart Express, where I'll be sharing the ups and downs of the development cycle as seen by a one-man show on his first program.
There'll be a lot of hair being pulled out as I struggle with crowd control algorithms, menu systems and misplaced mouse clicks, but I hope the occasional cheer as I *finally* work out how to keep persistent menu contents throughout the game!
In any case, it's there for you to look at, either to watch the development cycle in all its guts and glory and learn from it or to point and laugh.