void SpawnAnAsteroid(Vector3 asteroidPosition, float scale) { Vector3 spawnPosition = asteroidPosition; // Before we spawn anything let's check if object will collide with something, so we can adjust spawn position Collider[] colliders = Physics.OverlapSphere(spawnPosition, scale); foreach (Collider collider in colliders) { // Let's check if the new spawn point collides with this object if (collider.bounds.Contains(spawnPosition)) { // Get the collision direction Vector3 collisionDirection = (collider.transform.position - spawnPosition).normalized; // Get the distance from two objects float distance = Vector3.Distance(collider.transform.position, spawnPosition); // Move away from this collider spawnPosition += collisionDirection * distance; } } // Get random mesh for the new asteroid from the loaded meshes found. Mesh asteroidMesh = asteroidMeshes[Random.Range(0, asteroidMeshes.Count)]; // Get a new asteroid object from the pool GameObject asteroid = objectPoolerManager.SpawnFromPool(ObjectPoolerManager.ObjectType.Asteroid, spawnPosition, Quaternion.identity); asteroid.GetComponent <MeshFilter>().mesh = asteroidMesh; asteroid.transform.localScale = Vector3.one * scale; asteroid.GetComponent <MeshCollider>().sharedMesh = null; asteroid.GetComponent <MeshCollider>().sharedMesh = asteroidMesh; asteroids.Add(asteroid); }
private void Update() { if (isShooting) { if (shootTimer >= shootDelay) { // Nice we can shoot now, get bullet from pool, set it in front of the space ship and make the forward direction the same as the spaceship Vector3 position = transform.position + transform.forward * 0.5f; var bullet = objectPoolerManager.SpawnFromPool(ObjectPoolerManager.ObjectType.Bullet, position, Quaternion.identity); bullet.transform.forward = transform.forward; shootTimer = 0f; } else { shootTimer += Time.deltaTime; } } }