Exemple #1
0
    protected void InstantiateCameras(int carsCount)
    {
        float widtDelta     = (carsCount > 1) ? 0.5f : 1f;
        int   verticalCount = Mathf.CeilToInt(carsCount / 2f);
        float heightDelta   = 1.0f / verticalCount;

        for (int i = 0; i < carsCount; i++)
        {
            CameraFollower currentCameraFollower = GameObject.Instantiate(cameraFollowerPrefab).GetComponent <CameraFollower>();
            Camera         currentCamera         = currentCameraFollower.GetComponentInChildren <Camera>();
            int            horizontalIndex       = (i % 2);
            int            verticalIndex         = Mathf.FloorToInt((carsCount - 1 - i) / 2f);

            //Rect is defined by 4 points(corners) or by starting points and rect. The second one will be easier
            float startingX = horizontalIndex * widtDelta;
            float startingY = verticalIndex * heightDelta;
            currentCamera.rect = new Rect(startingX, startingY, widtDelta, heightDelta);

            currentCameraFollower.setTarget(cars[i].carController.transform);
            cars[i].cameraFollower = currentCameraFollower;
        }
    }