/// <summary> /// Raises the collision enter 2D event. /// </summary> private void OnCollisionEnter2D(Collision2D collision) { Collider2D col = collision.collider; // Check if space rock SpaceRock spaceRock = col.GetComponent <SpaceRock>(); if (spaceRock != null) { // Collide with rock Vector3 collisionVec = Vector3.Normalize(col.transform.position - this.transform.position); spaceRock.Collide(collisionVec); // Make spaceman move in opposite direction m_moveVec = -collisionVec *Mathf.Min(m_moveVec.magnitude, m_bumpSpeed); } // Check if helmet Helmet helmet = col.GetComponent <Helmet>(); if (helmet != null) { m_sceneMaster.NotifyGetHelmet(); } }
/// <summary> /// Initializes space rocks. /// </summary> private void InitializeSpaceRocks() { // Rocks would typically be spawned in the center of the screen (though it depends on inspector values). // Shift them all up or down to make sure there is an unblocked path from the spaceman to the helmet. bool shiftRocksUp = Random.value > 0.5f ? true : false; for (int spaceRockCount = 0; spaceRockCount < m_spaceRocks.Length; ++spaceRockCount) { SpaceRock spaceRock = m_spaceRocks[spaceRockCount]; if (spaceRockCount < m_spaceRocksPerLevel[m_level]) { // Randomize space rock position Vector2 spaceRockPos = new Vector2(Random.Range(m_spaceRockSpawnAreaMin.x, m_spaceRockSpawnAreaMax.x), Random.Range(m_spaceRockSpawnAreaMin.y, m_spaceRockSpawnAreaMax.y)); // Make sure there is enough space between space rocks bool isSpaced = true; float minDistSq = m_minDistBetweenSpaceRocks * m_minDistBetweenSpaceRocks; for (int x = 0; x < spaceRockCount; ++x) { if ((m_spaceRocks[x].transform.position - (Vector3)spaceRockPos).sqrMagnitude < minDistSq) { isSpaced = false; break; } } // If not spaced enough from other space rocks, re-randomize a new position if (!isSpaced) { spaceRockCount--; continue; } // Shift position up or down spaceRockPos.y += m_spaceRockShiftFromSpawnArea * (shiftRocksUp ? 1.0f : -1.0f); // Enable and initialize space rock spaceRock.transform.position = spaceRockPos; spaceRock.gameObject.SetActive(true); spaceRock.Initialize(); } else { // Make sure the rest of the space rocks are disabled spaceRock.gameObject.SetActive(false); } } }
/// <summary> /// Raises the trigger enter 2D event. /// </summary> private void OnCollisionEnter2D(Collision2D collision) { Collider2D col = collision.collider; // Check collision with other space rocks SpaceRock spaceRock = col.GetComponent <SpaceRock>(); if (spaceRock != null) { // Collide with rock Vector3 collisionVec = Vector3.Normalize(col.transform.position - this.transform.position); spaceRock.Collide(collisionVec); // Make this rock move the opposite direction m_moveVec = -collisionVec * m_speed; } }
void Update() { if (active) { if (nextSpawnIncrease < Time.time) { nextSpawnIncrease = Time.time + spawnIncreaseRate; spawnRate *= 1.01f; } if (Random.value < spawnRate) { spaceRocks.Add(Instantiate(comet, new Vector3(Random.value - 0.5f, Random.value - 0.5f, 0).normalized * 10f, Quaternion.identity)); } if (Random.value < spawnRate) { spaceRocks.Add(Instantiate(asteroid, new Vector3(Random.value - 0.5f, Random.value - 0.5f, 0).normalized * 10f, Quaternion.identity)); } } RaycastHit hit; Ray ray = camera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { if (Input.GetMouseButton(0)) { Planet p = hit.transform.GetComponent <Planet>(); if (p) { p.Hold(); } } if (Input.GetMouseButtonDown(0)) { Planet p = hit.transform.GetComponent <Planet>(); if (p) { p.Click(); return; } SpaceRock sr = hit.transform.GetComponent <SpaceRock>(); if (sr) { sr.Click(); return; } Clickable c = hit.transform.GetComponent <Clickable>(); if (c) { c.Click(); return; } } else { Clickable c = hit.transform.GetComponent <Clickable>(); if (c) { c.Hover(); return; } } } if (Input.GetKeyDown(KeyCode.DownArrow)) { LowerHeat(); } if (Input.GetKeyDown(KeyCode.UpArrow)) { RaiseHeat(); } }