private void spawnAsteroidRandom(int index) { int i = Random.Range(0, ResourcePathConstants.ASTEROIDS.Length); GameObject asteroid = Resources.Load(ResourcePathConstants.ASTEROIDS[i]) as GameObject; if (asteroid == null) { Debug.Log("Asteroid resource is null!"); } bool collision; Vector3 spawnLocation; CameraMeasurements camera = new CameraMeasurements(); do { float x = Random.Range(camera.getHorizontalMin(), camera.getHorizontalMax()); float y = Random.Range(camera.getVerticalMin(), camera.getVerticalMax()); spawnLocation = new Vector3(x, y, 0); collision = checkCollision(spawnLocation, asteroid); } while (collision); float rotation = Random.Range(0, 360); GameObject clone = (GameObject)Instantiate(asteroid, spawnLocation, Quaternion.Euler(0, 0, rotation)); clone.name = "Asteroid_" + (index + 1); game.Add(clone); }
private void moveToFinalPosition() { if (!finalPositionDetermined) { //Chose Drop Item if exists or random Position CameraMeasurements camera = new CameraMeasurements(); float x = Random.Range(camera.getHorizontalMin() + 0.5f, camera.getHorizontalMax() - 0.5f); float y = Random.Range(camera.getVerticalMin() + 0.5f, camera.getVerticalMax() - 0.5f); Transform dropItem = dropItemPosition(); finalPosition = dropItem ? dropItem.position : new Vector3(x, y, 0); finalPositionDetermined = true; } MoveTowards(finalPosition); RotateTowards(finalPosition); if (transform.position == finalPosition) { FindObjectOfType <AudioManager>().Stop("engine1"); finalPositionDetermined = false; roundFinished = true; timer = 3f; alreadyShot = false; } }
//Returns true if a dropLocation was found private bool calculateDropLocation() { CameraMeasurements camera = new CameraMeasurements(); bool collision; float borderDistance = 0.75f; int counter = 0; do { counter++; float x = Random.Range(camera.getHorizontalMin() + borderDistance, camera.getHorizontalMax() - borderDistance); float y = Random.Range(camera.getVerticalMin() + borderDistance, camera.getVerticalMax() - borderDistance); this.dropLocation = new Vector3(x, y, 0); Debug.Log("dropLocation: x=" + x + " y=" + y); collision = checkItemCollision(); if (counter == 10000) { return(false); } } while (collision); return(true); }
private void placeShip() { float outOfViewRange = 3f; float x = this.dropLocation.x; float y = this.dropLocation.y; CameraMeasurements camera = new CameraMeasurements(); float xb1 = camera.getHorizontalMax() + outOfViewRange; float xb2 = camera.getHorizontalMin() - outOfViewRange; float yb1 = camera.getVerticalMax() + outOfViewRange; float yb2 = camera.getVerticalMin() - outOfViewRange; Vector2[] possiblePositions = { new Vector2(x, yb1), new Vector2(x, yb2), new Vector2(xb1, y), new Vector2(xb2, y) }; this.startLocation = possiblePositions[Random.Range(0, 3)]; Debug.Log("startLocation: x=" + startLocation.x + " y=" + startLocation.y); float rotation; if (this.startLocation.x == xb1) { rotation = 90; } else if (this.startLocation.x == xb2) { rotation = 270; } else if (this.startLocation.y == yb1) { rotation = 180; } else { rotation = 0; } this.ship = (GameObject)Instantiate(this.ship, this.startLocation, Quaternion.Euler(0, 0, rotation)); if (!GlobalVariables.local) { NetworkServer.Spawn(ship); } this.ship.name = "Support_Ship"; SupportShipAction action = this.ship.GetComponent <SupportShipAction>(); action.startPosition = this.startLocation; action.dropPosition = this.dropLocation; }
private void spawnShipRandom(string path, int index) { GameObject ship = Resources.Load(path) as GameObject; CameraMeasurements camera = new CameraMeasurements(); if (ship == null) { Debug.Log("Ship resource is null!"); } bool collision; Vector3 spawnLocation; do { float x = Random.Range(camera.getHorizontalMin() + borderDistance, camera.getHorizontalMax() - borderDistance); float y = Random.Range(camera.getVerticalMin() + borderDistance, camera.getVerticalMax() - borderDistance); spawnLocation = new Vector3(x, y, 0); collision = checkCollision(spawnLocation, ship); } while (collision); float rotation = Random.Range(0, 360); GameObject clone = (GameObject)Instantiate(ship, spawnLocation, Quaternion.Euler(0, 0, rotation)); if (path.Equals(ResourcePathConstants.SHIP_EARTH)) { clone.name = "Ship_Earth_" + (index + 1); ShipContainer.earthShips.Add(clone); } else { clone.name = "Ship_Mars_" + (index + 1); ShipContainer.marsShips.Add(clone); //Enable AI if singleplayer if (GlobalVariables.singlePlayer) { clone.GetComponent <ProtoMovement>().enabled = false; clone.GetComponent <Shoot>().enabled = false; clone.GetComponent <AIBehaviour>().enabled = true; } else { clone.GetComponent <AIBehaviour>().enabled = false; } } game.Add(clone); }
public void createBackgroundPlanet() { int i = Random.Range(0, ResourcePathConstants.PLANETS.Length); GameObject planet = Resources.Load(ResourcePathConstants.PLANETS[i]) as GameObject; if (planet == null) { Debug.Log("Planet resource is null!"); } CameraMeasurements camera = new CameraMeasurements(); float x = Random.Range(camera.getHorizontalMin() + borderDistance, camera.getHorizontalMax() - borderDistance); float y = Random.Range(camera.getVerticalMin() + borderDistance, camera.getVerticalMax() - borderDistance); Vector3 spawnLocation = new Vector3(x, y, 0); float rotation = Random.Range(0, 360); GameObject clone = (GameObject)Instantiate(planet, spawnLocation, Quaternion.Euler(0, 0, rotation)); float scale = Random.Range(0.05f, 0.15f); clone.transform.localScale = new Vector3(scale, scale, 0); clone.name = "Planet"; //Applying shadow to planet i = Random.Range(0, ResourcePathConstants.PLANET_SHADOWS.Length); GameObject shadow = Resources.Load(ResourcePathConstants.PLANET_SHADOWS[i]) as GameObject; if (shadow == null) { Debug.Log("Planet resource is null!"); } spawnLocation = new Vector3(x, y, 0); rotation = Random.Range(0, 360); GameObject clone2 = (GameObject)Instantiate(shadow, spawnLocation, Quaternion.Euler(0, 0, rotation)); clone2.transform.localScale = new Vector3(scale, scale, 0); clone2.name = "Shadow"; game.Add(clone); game.Add(clone2); }
public void keepObjectInCameraView() { gameCamera = new CameraMeasurements(); if ((transform.position.x >= gameCamera.getHorizontalMax())) { transform.position = new Vector3(gameCamera.getHorizontalMax(), transform.position.y, 0); } if ((transform.position.x <= gameCamera.getHorizontalMin())) { transform.position = new Vector3(gameCamera.getHorizontalMin(), transform.position.y, 0); } if ((transform.position.y >= gameCamera.getVerticalMax())) { transform.position = new Vector3(transform.position.x, gameCamera.getVerticalMax(), 0); } if ((transform.position.y <= gameCamera.getVerticalMin())) { transform.position = new Vector3(transform.position.x, gameCamera.getVerticalMin(), 0); } }