void CreateSmallerAsteroid(float radiusDivider) { GameObject smallerAsteroid = Instantiate(this.asteroidPrefab, transform.position, Quaternion.identity); AsteroidScript asteroidScript = smallerAsteroid.GetComponent <AsteroidScript>(); asteroidScript.Initialize(this.radius / radiusDivider); }
void OnCollisionEnter(Collision collision) { // the Collision contains a lot of info, but it’s the colliding // object we’re most interested in. Collider collider = collision.collider; if (collider.CompareTag("Asteroid")) { // update score ScoreUI.score += 10; AsteroidScript roid = collider.gameObject.GetComponent <AsteroidScript>(); // let the other object handle its own death throes roid.Die(); // Destroy the Bullet which collided with the Asteroid Destroy(gameObject); } else { // if we collided with something else, print to console // what the other thing was Debug.Log("Collided with " + collider.tag); } }
void SpawnAsteroid (Vector3 spawnModifier, Vector3 impactForce) { // Obtain a random prefab GameObject asteroid = RandomPrefab(); if (asteroid) { // Instantiate the asteroid asteroid = (GameObject) Network.Instantiate (asteroid, transform.position + spawnModifier, transform.rotation, 0); // Add the impact force to keep the asteroid moving asteroid.rigidbody.velocity = rigidbody.velocity * m_velocityToMaintain; asteroid.rigidbody.AddForce (impactForce); // Scale the asteroid correctly AsteroidScript script = asteroid.GetComponent<AsteroidScript>(); if (script) { script.TellToPropagateScaleAndMass (transform.localScale / m_splittingFragments, rigidbody.mass / m_splittingFragments); script.DelayedVelocitySync (Time.fixedDeltaTime); script.isFirstAsteroid = false; } } else { Debug.LogError ("AsteroidScript couldn't generate a random prefab for splitting."); } }
/// <summary> /// Inflicts damage, flash sprite, and check if the object should be destroyed /// </summary> /// <param name="damageCount"></param> /// public void Damage(int damageCount) { if (!active) { return; } hp -= damageCount; sr.material = matWhite; //Flash white if (hp <= 0) { // Dead! if (deathExplosion != null) { var deathExplosionTransform = Instantiate(deathExplosion) as Transform; deathExplosionTransform.position = transform.position; } //Asteroids may break apart astScr = GetComponentInChildren <AsteroidScript>(); if (astScr != null) { astScr.BreakApart(); } Destroy(gameObject); } else { //If not dead, return from white flash after interval Invoke("ResetMaterial", flashInterval); } }
void Start() { player = GameObject.FindGameObjectWithTag("Player"); playerController = player.GetComponent <PlayerController> (); finger = GameObject.FindGameObjectsWithTag("Ink"); trail = finger[0].GetComponent <AsteroidScript> (); perksName = ""; guiStyle = new GUIStyle(); guiStyle.fontSize = 1; }
// Asteroids need to sync their velocity over the network after waiting for a FixedUpdate() call void SyncAsteroid(Rigidbody asteroid) { AsteroidScript script = asteroid.GetComponent <AsteroidScript>(); if (script) { // Wait one FixedUpdate frame to sync the asteroids script.DelayedVelocitySync(Time.fixedDeltaTime); } }
public void Initialize(float radius) { this.radius = radius; this.isCrushableToSmallerAsteroids = AsteroidScript.IsCrushableFromRadius(radius); damageReceiver.health = AsteroidScript.HitPointsFromRadius(radius); Vector3[] asteroidVertices = GenerateAsteroidVerticles(); SetupLineRenderer(asteroidVertices); SetupPolygonCollider2D(asteroidVertices); SetupRigidBody2D(asteroidVertices); }
/** @brief GameState transitioned from GameStart to GamePlay. */ void BeginPlay() { this.SpawnShip(this.shipPrefab); // Spawn large asteroids for (uint i = 0; i < this.currentLevel * 3; i++) { float startingX = (Random.value - 0.5f) * Globals.screenWidth; float startingY = (Random.value - 0.5f) * Globals.screenHeight; Vector3 pos = new Vector3(startingX, startingY, 0.0f); GameObject newAsteroid = Instantiate(this.asteroidLargePrefab) as GameObject; newAsteroid.transform.parent = this.guiAsteroids.transform; newAsteroid.transform.localPosition = pos; newAsteroid.name = "LargeAsteroid" + i.ToString(); AsteroidScript newAsteroidScript = newAsteroid.GetComponent <AsteroidScript>(); if (newAsteroidScript != null) { newAsteroidScript.SetSpeed((float)this.currentLevel * 2.0f); newAsteroidScript.SetRotSpeed(60.0f, 60.0f, 60.0f); } this.AddAsteroid(newAsteroid); } // Spawn medium asteroids for (uint i = 0; i < this.currentLevel * 3; i++) { float startingX = (Random.value - 0.5f) * Globals.screenWidth; float startingY = (Random.value - 0.5f) * Globals.screenHeight; Vector3 pos = new Vector3(startingX, startingY, 0.0f); GameObject newAsteroid = Instantiate(this.asteroidMediumPrefab) as GameObject; newAsteroid.transform.parent = this.guiAsteroids.transform; newAsteroid.transform.localPosition = pos; newAsteroid.name = "MediumAsteroid" + i.ToString(); AsteroidScript newAsteroidScript = newAsteroid.GetComponent <AsteroidScript>(); if (newAsteroidScript != null) { newAsteroidScript.SetSpeed((float)this.currentLevel * 2.0f); newAsteroidScript.SetRotSpeed(60.0f, 60.0f, 60.0f); } this.AddAsteroid(newAsteroid); } GameStateScript.gameState = GameState.GamePlay; }
/** * @brief Asteroid has collided with either a shot or the player's ship. */ void OnCollisionEnter() { // Call the DestroyAsteroidDelegate delegate this.blowItUp(); if (this.gcs != null) { this.gcs.AddPoints(points); } // If we attached any asteroid prefabs to 'asteroidPrefab', then the // current asteroid breaks apart when hit into several smaller ones. if (this.asteroidPrefab != null) { // Destroy the current gameObject, and create smaller asteroids in its place for (uint i = 0; i < this.numAsteroidsToSpawn; i++) { float startingX = transform.position.x + Random.Range(-5.0f, 5.0f); float startingY = transform.position.y + Random.Range(-5.0f, 5.0f); Vector3 pos = new Vector3(startingX, startingY, transform.position.z); GameObject newAsteroid = Instantiate(this.asteroidPrefab) as GameObject; newAsteroid.transform.position = pos; newAsteroid.name = "Asteroid" + i; // Slightly higher velocity than the original asteroid newAsteroid.rigidbody.velocity = this.gameObject.rigidbody.velocity * 1.5f; AsteroidScript newAsteroidScript = newAsteroid.GetComponent <AsteroidScript>(); if (newAsteroidScript != null) { newAsteroidScript.rotationSpeed = this.rotationSpeed; } // Parent new asteroid to correct layer this.gcs.AddToAsteroidsLayer(newAsteroid); // Add new asteroid(s) into gcs's array for management this.gcs.AddAsteroid(asteroidPrefab); } } Instantiate(explosionPrefab, transform.position, transform.rotation); // Remove current asteroid from gcs's array this.gcs.RemoveAsteroid(gameObject); }
void OnCollisionEnter2D(Collision2D collision) { bool damagePlayer = false; // Collision with enemy EnemyScript enemy = collision.gameObject.GetComponent <EnemyScript>(); if (enemy != null) { // Kill the enemy HealthScript enemyHealth = enemy.GetComponent <HealthScript>(); if (enemyHealth != null) { enemyHealth.Damage(1); } damagePlayer = true; } AsteroidScript asteroid = collision.gameObject.GetComponent <AsteroidScript>(); if (asteroid != null) { // Kill the enemy HealthScript asteroidHealth = asteroid.GetComponent <HealthScript>(); if (asteroidHealth != null) { asteroidHealth.Damage(1); } damagePlayer = true; } // Damage the player if (damagePlayer) { HealthScript playerHealth = this.GetComponent <HealthScript>(); if (playerHealth != null) { playerHealth.Damage(1); } } }
public void AddAsteroid(GameObject asteroidObj) { if (asteroidObj == null) { Debug.Log("Could not add asteroid: asteroid was null."); return; } // Ensure that it's actually an asteroid by the presence of the AsteroidScript component AsteroidScript asteroidObjScript = asteroidObj.GetComponent <AsteroidScript>(); if (asteroidObjScript != null) { this.asteroids.Add(asteroidObj); } else { Debug.Log("Object is not an asteroid."); } }
// Detecting collisions against the ship using an Unity function. private void OnCollisionEnter(Collision collision) { // Making sure that the collision object is an Asteroid using an `if statement`. if (collision.gameObject.name == "Asteroid_01_S(Clone)") // Detecting the collision between the Ship and Asteroid. { GameObject asteroid = GameObject.FindWithTag("Asteroid"); // Get Asteroid GameObject, to access the Asteroid List. if (asteroid != null) // In case that the Asteroid GameObject was found. { AsteroidScript _asteroidScript = asteroid.GetComponent <AsteroidScript>(); // Get AsteroidScript by Asteroid GameObject. aExplosion = Instantiate(explosion, collision.gameObject.transform.position, Quaternion.identity); // Creating a clone of the explosion and place it in the collision position. _asteroidScript.asteroids.Remove(collision.gameObject); // Removing asteroid from the list to avoid Missing Reference Exception. Destroy(collision.gameObject); // Destroying the asteroid after the collision in the screen. } LoadSceneScript loadScene = new LoadSceneScript(); // Instantiating LoadSceneScript to change scenes. loadScene.ChangeScene(2); // Changing to EndScene after collision. } }
// Detecting collisions related to the bullet using an Unity function. private void OnCollisionEnter(Collision collision) { // Making sure that object collision is an Asteroid. if (collision.gameObject.name == "Asteroid_01_S(Clone)") // Detecting the collision between Asteroid and Bullet. { ShipScript.score++; // Add 1 to score. GameObject asteroid = GameObject.FindWithTag("Asteroid"); // Getting the Game Object with the tag "Asteroid". if (asteroid != null) // Checking if asteroid gameObject was found. { AsteroidScript _asteroidScript = asteroid.GetComponent <AsteroidScript>(); // Getting the asteroid script. aExplosion = Instantiate(explosion, collision.gameObject.transform.position, Quaternion.identity); // Creating an explosion and placing in collision position _asteroidScript.asteroids.Remove(collision.gameObject); // Removing asteroid from the list to avoid Missing Reference Exception. Destroy(collision.gameObject); // Destroying the asteroid from the screen. Destroy(gameObject); // Destroying the bullet. } } }
void SpawnAsteroid() { float asteroidX = 0.0f; float asteroidY = 0.0f; const int maximumTries = 10; for (int i = 0; i < maximumTries; i++) { asteroidX = Random.Range(-this.asteroidSpawnBoundBox.x, this.asteroidSpawnBoundBox.x); asteroidY = Random.Range(-this.asteroidSpawnBoundBox.y, this.asteroidSpawnBoundBox.y); if (Vector2.Distance(player.position, new Vector2(asteroidX, asteroidY)) > minimumDistanceToPlayer) { break; } } GameObject asteroid = Instantiate(asteroidPrefab, new Vector3(asteroidX, asteroidY, 0.0f), Quaternion.identity); AsteroidScript asteroidScript = asteroid.GetComponent <AsteroidScript>(); const float maxRadius = 5.0f; const float minRadius = 2.0f; float asteroidRadius = UnityEngine.Random.Range(minRadius, maxRadius); asteroidScript.Initialize(asteroidRadius); }
IEnumerator SpawnWaves() { yield return(new WaitForSeconds(startWait)); while (true) { for (int i = 0; i < asteroidCount; i++) { Vector3 spawnPosition = new Vector3(Random.Range(-5, 5), Random.Range(-5, 5), spawnValues.z); Quaternion spawnRotation = Quaternion.identity; Object asteroidObject = Instantiate(asteroid, spawnPosition, spawnRotation); GameObject asteroidGameObject = (GameObject)asteroidObject; AsteroidScript asteroidScript = asteroidGameObject.GetComponent <AsteroidScript>(); int num = Random.Range(1, 5); if (num == 1) { myBehavior = new JitterMovement(); } else if (num == 2) { myBehavior = new StraightMovement(); } else if (num == 3) { myBehavior = new AngleMovement(); } else if (num == 4) { myBehavior = new FastMovement(); } asteroidScript.myMovement = myBehavior; yield return(new WaitForSeconds(spawnWait)); } yield return(new WaitForSeconds(waveWait)); } }
IEnumerator AsteroidSpawner() { float WaitTime = 6.5f; for (; !GameOver;) { if (GamePaused || !AsteroidSpawning) { yield return(new WaitForEndOfFrame()); } else { GameObject tmp = SpawnEnemy(0); AsteroidScript tmpasteroid = tmp.GetComponent <AsteroidScript>(); switch ((int)Random.Range(1f, 101f)) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: tmp.transform.localScale = new Vector3(1f, 1f, 1f); tmpasteroid.HP = 4; tmpasteroid.Score = 50; tmpasteroid.ExplosionScale = 2f; break; case 11: tmp.transform.localScale = new Vector3(2f, 2f, 2f); tmpasteroid.HP = 6; tmpasteroid.Score = 250; tmpasteroid.ExplosionScale = 3f; break; default: break; } for (int i = 0; i < 101; i++) { if (GamePaused) { i--; yield return(new WaitForEndOfFrame()); } else { yield return(new WaitForSeconds(WaitTime / 100)); } } if (WaitTime > 2f) { WaitTime -= 0.05f; } } } yield return(new WaitForEndOfFrame()); }
void Awake() { asteroidScript = gameObject.GetComponent <AsteroidScript>(); maxHp = hp; }
public Asteroid(GameObject go) { this.go = go; this.script = go.GetComponent <AsteroidScript>(); }
/** * @brief Scene showed to players before an active game session. * @details This should show the game title, Asteroids, have an option to * PLAY GAME or see HIGH SCORES. There will be asteroids flying around * randomly in the background. */ private void GameStart() { // Reusing this is okay, since each reference (attaching to a parent // transform, for example) retains the GameObject. GameObject guiGO; // Large ASTEROIDS title: GO and its component guiGO = new GameObject("guiAsteroidsTitle"); guiGO.transform.parent = this.guiElements.transform; guiGO.transform.localPosition = new Vector3(0.5f, 0.65f, 0.0f); guiGameStartTitle = guiGO.AddComponent <GUIText>(); guiGameStartTitle.text = "ASTEROIDS"; guiGameStartTitle.anchor = TextAnchor.MiddleCenter; guiGameStartTitle.alignment = TextAlignment.Center; guiGameStartTitle.pixelOffset = Vector2.zero; guiGameStartTitle.lineSpacing = 1.0f; guiGameStartTitle.tabSize = 4.0f; guiGameStartTitle.fontSize = 50; guiGameStartTitle.fontStyle = FontStyle.Bold; // Smaller "Press SPACE" text: GO and its component guiGO = new GameObject("guiPressSpace"); guiGO.transform.parent = this.guiElements.transform; guiGO.transform.localPosition = new Vector3(0.5f, 0.4f, 0.0f); guiGameStartPlay = guiGO.AddComponent <GUIText>(); guiGameStartPlay.text = "Press SPACE to begin"; guiGameStartPlay.anchor = TextAnchor.MiddleCenter; guiGameStartPlay.alignment = TextAlignment.Center; guiGameStartPlay.pixelOffset = Vector2.zero; guiGameStartPlay.lineSpacing = 1.0f; guiGameStartPlay.tabSize = 4.0f; guiGameStartPlay.fontSize = 20; guiGameStartPlay.fontStyle = FontStyle.Normal; // Bunch of random asteroids flying around. These will not be used in // the actual game; they are just austhetics while we wait for the // player to hit SPACE. int numAsteroids = 30; // All asteroids here will be set to increasing z-depths so they don't // look funky as they overlap each other. float startingZ = 0.0f; for (; startingZ < (float)(numAsteroids / 3.0f); startingZ += 1.0f) { float startingX = (Random.value - 0.5f) * Globals.screenWidth; float startingY = (Random.value - 0.5f) * Globals.screenHeight; Vector3 pos = new Vector3(startingX, startingY, startingZ); GameObject newAsteroid = Instantiate(this.asteroidLargePrefab) as GameObject; newAsteroid.transform.parent = this.guiAsteroids.transform; newAsteroid.transform.localPosition = pos; newAsteroid.name = "LargeAsteroid" + Mathf.Floor(startingZ); AsteroidScript newAsteroidScript = newAsteroid.GetComponent <AsteroidScript>(); if (newAsteroidScript != null) { newAsteroidScript.SetSpeed((float)this.currentLevel * 2.0f); newAsteroidScript.SetRotSpeed(60.0f, 60.0f, 60.0f); } this.AddAsteroid(newAsteroid); } // Resume at previous z-depth for (; startingZ < (float)(numAsteroids * 2.0f / 3.0f); startingZ += 1.0f) { float startingX = (Random.value - 0.5f) * Globals.screenWidth; float startingY = (Random.value - 0.5f) * Globals.screenHeight; Vector3 pos = new Vector3(startingX, startingY, startingZ); GameObject newAsteroid = Instantiate(this.asteroidMediumPrefab) as GameObject; newAsteroid.transform.parent = this.guiAsteroids.transform; newAsteroid.transform.localPosition = pos; newAsteroid.name = "MediumAsteroid" + Mathf.Floor(startingZ); AsteroidScript newAsteroidScript = newAsteroid.GetComponent <AsteroidScript>(); if (newAsteroidScript != null) { newAsteroidScript.SetSpeed((float)this.currentLevel * 2.0f); newAsteroidScript.SetRotSpeed(60.0f, 60.0f, 60.0f); } this.AddAsteroid(newAsteroid); } // Keep going at previous z-depth for (; startingZ < (float)numAsteroids; startingZ += 1.0f) { float startingX = (Random.value - 0.5f) * Globals.screenWidth; float startingY = (Random.value - 0.5f) * Globals.screenHeight; Vector3 pos = new Vector3(startingX, startingY, startingZ); GameObject newAsteroid = Instantiate(this.asteroidSmallPrefab) as GameObject; newAsteroid.transform.parent = this.guiAsteroids.transform; newAsteroid.transform.localPosition = pos; newAsteroid.name = "SmallAsteroid" + Mathf.Floor(startingZ); AsteroidScript newAsteroidScript = newAsteroid.GetComponent <AsteroidScript>(); if (newAsteroidScript != null) { newAsteroidScript.SetSpeed((float)this.currentLevel * 2.0f); newAsteroidScript.SetRotSpeed(60.0f, 60.0f, 60.0f); } this.AddAsteroid(newAsteroid); } GameStateScript.UserInputPlay += UserInputPlay; }
//this is only called once at initialization. public void setPointOfOrbit(Transform point) { spinner = GetComponent<AsteroidScript>(); spinner.setPointOfOrbit(point); }