Dodgy Drivers Part 1
Game Jam Time! This week I will be partaking in a 2 and a half day game jam. The theme for this jam is local multiplayer and I have convinced my team to make a game based on a small prototype I had previously made.
The first aspect of this game I worked on was the camera functionality. The style of the game requires the camera to stay focused on whoever is currently in first place and "kill" whoever falls of the screen.
There were two main aspects to this, camera movement and player destruction
To get the camera to follow the winner I made an array of GameObjects called cars, at the beginning of the game whenever players are added into the game they are also added into this array. As the game updates the camera constantly attempts to update its own position. It does this by using a function called UpdateCameraPos() which calls another function called FindFirstPlace() which checks to see which car is in first place is by iterating through the cars array and compares the "Z" values of each car and returns whichever one was highest. This works as in this prototype of the game the players only have to move forward, if I was to make it a more advanced version of the game I would most likely check to see which player was closest to the nearest checkpoint using magnitude. The FindFirstPlace() function then returns the GameObject that was at the front and UpdateCameraPos() then sets its own z position to be equal to that of the current leader.
The player Death itself was actually surprisingly simple and the entirety of the code for it can be seen below
void Start () { GameCamera = FindObjectOfType<Camera>(); } void Update () { Vector3 screenPos = GameCamera.WorldToScreenPoint(this.transform.position);
if (screenPos.y <= -50 || transform.position.y < -100) { this.gameObject.SetActive(false); }
}
as this shows, all that I had to do was use Unity's in built WorldToScreenPoint function and check if the car itself was still on the screen by this method. If the car was off the bottom of the screen i simply had the car turn itself off. I also added a check to destroy the car if it fell too far below the world in case it glitched through the level geometry.