How can I instantiate projectiles to have random inaccuracy?

Hi all,

I’m currently working on a project where the player must defend Earth from asteroids and eventually, Apophis.

I’ve created a system where there are a number of “asteroid spawn points” which will fire asteroids at Earth, but with a degree of inaccuracy so that not every asteroid will strike the planet. I’ve already completed the logic for randomly assigning when and which spawn points will fire, but I am having trouble with the whole firing part. I’m not sure how to instantiate one aimed at a random point, or really how to approach this.

My code below:

function Update() {

	// Check if I have been selected and if a Fire Request is present
	if (AC.spawnAsteroid && AC.fromSpawner == spawnerName){
		
		Fire();
	
	}

}

function Fire() {

	// Instantiate an Asteroid with a random chance of striking Earth
	

	// Clear Asteroid Control's Fire Requests
	AC.spawnAsteroid = false;
	AC.fromSpawner = "asdf";
	
	// Notify Debug Log of action
	Debug.Log(spawnerName + " firing asteroid!");

}

Project screenshot: dropbox

As if you were looking from the spawner in first person: also dropbox

The player rotates around the world by clicking and dragging, and can click to “attack” asteroids that they deem will hit the planet.

Thanks in advanced!

Note: This simulation is geocentric; Earth doesn’t actually move, the sun and skybox rotate around it.

One way way would be to generate a random point inside the sphere represented by the outside edge of your red zone, and use that point as the target. Untested example code:

var goEarth : GameObject;
var goPrefab : GameObject;
var enlargeFactor : float = 1.5f;

function Fire()
{
	v3Pos = Random.insideUnitSphere * goEarth.transform.localScale * enlargeFactor;
	v3Pos += goEarth.transform.position;
	transform.LookAt(v3Pos);
	var go : GameObject = Instantiate(goPrefab, transform.position, transform.rotation);
	go.rigidbody.AddRelativeForce(Vector3.forward * 2000.0);
}

enlargeFactor should take the sphere to the edge of the red zone.