Spawning Placement

Hi! I’m making a script that controls the direction and rotation of a meteor. The part that I need help on is what it does when it is hit with a bullet.

I want the asteroid to spawn 4 other asteroids, all 1/4 of its scale, after being shot with a bullet. That was easy enough. The problem is that I need to make all four little asteroids spawn in different locations around the original asteroid an not on top of each other. My script:

var Speed = 1.0;
var MinSize = 1.0;
var MaxSize = 1.0;
var asteroids : GameObject[];
var scale : float = Random.Range(MinSize,MaxSize);
function Awake(){

transform.rotation = Random.rotation;
transform.localScale = Vector3(scale,scale,scale);
}
function Update () {
	transform.Translate(0,0,-Speed,Space.World);
	transform.Rotate(1,1,5);

}

function OnCollisionEnter(collision : Collision){
	MakeMoreThenDie(scale);
}

function MakeMoreThenDie(cheese){
	
	[COLOR="#ff0000"]var inst1pos = Vector3((transform.up +1), transform.forward, (transform.right -1));
	var inst2pos = Vector3((transform.up -1), transform.forward, (transform.right +1));
	var inst3pos = Vector3((transform.up +2), transform.forward, (transform.right-2));
	var inst4pos = Vector3((transform.up -2), transform.forward, (transform.right +2));[/COLOR]
	 var instance1 : GameObject = GameObject.Instantiate( asteroids[0],inst1pos, transform.rotation);
	 var instance2 : GameObject = GameObject.Instantiate( asteroids[1], inst2pos, transform.rotation);
	 var instance3 : GameObject = GameObject.Instantiate( asteroids[2], inst3pos, transform.rotation);
	 var instance4 : GameObject = GameObject.Instantiate( asteroids[3], inst4pos, transform.rotation);
	 
	 cheese = cheese/4;
	 
	 instance1.transform.localScale = Vector3(cheese,cheese,cheese);
	 instance2.transform.localScale = Vector3(cheese,cheese,cheese);
	 instance3.transform.localScale = Vector3(cheese,cheese,cheese);
	 instance4.transform.localScale = Vector3(cheese,cheese,cheese);
	Destroy(gameObject);
}

The game engine does not like that I am trying to do math with the positions.

So how would I make the new asteroids all spawn close to the original one?

Transform.up is (0,1,0) in transform local space.
.right is (1,0,0), .forward is (0,0,1).

Use transform.up, -transform.up, etc. directly instead of trying to create a vector3 of vector3s.

This:

	var inst1pos = ((transform.up + transform.up*2), transform.forward,(transform.right + transform.right*2));
	var inst2pos = ((transform.up - transform.up*2), transform.forward,(transform.right - transform.right*2));
	var inst3pos = ((transform.up + transform.up), transform.forward,(transform.right-transform.right));
	var inst4pos = ((transform.up - transform.up*3), transform.forward,(transform.right + transform.right));

is still giving me errors like:

Seems to be it is expecting something else… Did I add too many parenthesis? Is this not the right way to code it?

How about just making a prefab that has four smaller asteroids already positioned so they are not on top of each other, and instantiate that when original asteroid is destroyed? For example create empty gameobject called… maybe smallasteroids, take asteroid ,scale it to 1/4 and dublicate it until you have four, parent them under smallasteroids, position them nicely and make it a prefab. Or maybe make empty gameobjects that serve as spawnpoints.

Personally I’am more art oriented person, but … maybe you’re trying to do this little complicated way and that generates context problems and errors.

Those errors… I think it is not about parenthesis but simply a context error, it is a Vector3.

The problem is when you hit one of the smaller asteroids, because then they will just duplicate themselves and not get smaller…

Good idea, though.

transform.right is a position.

var instpos1 = transform.position + transform.right;
var instpos2 = transform.position - transform.right;

etc.

Ooooh… Thanks!