// Defines spawn position based on prefab type and chosen lane private Vector3 SpawnPosition(GameObject prefab, StreetLane lane) { float yPosition; switch (prefab.tag) { case "Cone": yPosition = -0.5f; break; default: yPosition = 0f; break; } switch (lane) { case StreetLane.Left: return(new Vector3(-6f, yPosition, zPosition)); case StreetLane.Middle: return(new Vector3(0f, yPosition, zPosition)); case StreetLane.Right: return(new Vector3(6f, yPosition, zPosition)); default: return(new Vector3(0f, 0f, 0f)); } }
public GameObject PlaceVehicle(GameObject vehiclePrefab, Vector3 buildingCoordinates) { StreetLane lane = transform.InverseTransformPoint(buildingCoordinates).x > 0 ? StreetLane.Right : StreetLane.Left; _laneStep = transform.localScale.x / 8; _vehicleInstance = Instantiate(vehiclePrefab, transform.TransformPoint(new Vector3((int)lane * _laneStep, 3)), transform.localRotation); if (Math.Abs(transform.rotation.y) > 0.1f) { _vehicleInstance.transform.position = new Vector3(buildingCoordinates.x, _vehicleInstance.transform.position.y, _vehicleInstance.transform.position.z); _vehicleInstance.transform.rotation = Quaternion.Euler(new Vector3(0, -_vehicleInstance.transform.eulerAngles.y, 0)); } else { _vehicleInstance.transform.position = new Vector3(_vehicleInstance.transform.position.x, _vehicleInstance.transform.position.y, buildingCoordinates.z); } return(_vehicleInstance); }
// Thread for spwaning obstacles private IEnumerator Spawn() { yield return(new WaitForSeconds(2f)); System.Random random = new System.Random(); System.Array lanes = System.Enum.GetValues(typeof(StreetLane)); while (true) { GameObject prefab = obstaclePrefabs[random.Next(obstaclePrefabs.Length)]; StreetLane lane = (StreetLane)lanes.GetValue(random.Next(lanes.Length)); Vector3 position = SpawnPosition(prefab, lane); Quaternion rotation = SpawnRotation(prefab); Instantiate(prefab, position, rotation, transform); yield return(new WaitForSeconds(spawnInterval)); spawnInterval = System.Math.Max(spawnInterval - 1f, 5f); } }
// Update is called once per frame void Update() { // Updates speed speed += Input.GetAxis("Vertical") * acceleration * Time.deltaTime; speed -= friction * Time.deltaTime; speed = Mathf.Clamp(speed, 0, 20); speedText.text = "Speed: " + (int)(speed * 18 / 5) + " km/h"; if (Time.deltaTime > 0f) { // Moves according to user input Vector3 position = transform.position; if (Input.GetKeyUp("left") || Input.GetKeyUp("a")) { if (CameraWithinRange(270f, 360f) || CameraWithinRange(0f, 1.5f)) { position.x -= 6f; if (_lane == StreetLane.Middle) { _lane = StreetLane.Left; } else if (_lane == StreetLane.Right) { _lane = StreetLane.Middle; } } else if (position.x >= 0f) { gameController.GameOver("You must always check the rearview before making a turn!"); } } else if (Input.GetKeyUp("right") || Input.GetKeyUp("d")) { if (CameraWithinRange(19f, 90f)) { position.x += 6f; if (_lane == StreetLane.Middle) { _lane = StreetLane.Right; } else if (_lane == StreetLane.Left) { _lane = StreetLane.Middle; } } else if (position.x <= 0f) { gameController.GameOver("You must always check the rearview before making a turn!"); } } position.x = Mathf.Clamp(position.x, -6f, 6f); transform.position = position; } // Checks game over if (speed < 5f) { gameController.GameOver("You shouldn't move that slow on a highway!"); } }
// Start is called before the first frame update void Start() { gameController = FindObjectOfType(typeof(GameController)) as GameController; _lane = StreetLane.Middle; }