I have a script for a certain type of enemy, where when you shoot him, he turns into several smaller enemies.
When he gets hit by a certain object, this function is called:
function Split()
{
Destroy(gameObject);
for ( var i : int = 0; i < 10; i ++ )
{
Instantiate( split,transform.position + Vector3(i * 0, 0, 2.0), Quaternion.identity );
}
}
problem is, when the mini enemies spawn, they sort of explode away from the center. Like there is force being applied to them, even though there isnt.
There are some errors on your console, I think that you should resolve them, it can create some strange events on your game, also did you check the new objects’s gravity?
Do you have an understanding of cosine and sine, and the unit circle? If not read this: Unit circle wiki.
With this knowledge we can instantiate our objects in a circle around our base!
int size = 4; //Just set it to a number that fit the size of your objects
for (int i = 0; i < 10; i++)
{
//convert to radians
float angle = 36 * i;
float rad = angle * Mathf.PI / 180.0F;
float x = Mathf.Cos(rad);
float z = Mathf.Sin(rad);
Instantiate(obj, transform.position + new Vector3(x * size, 0, z * size), Quaternion.identity);
}
Not sure if thats C# or something, but not only is the syntax kind of strange looking to me, this script gives lots of errors when I try and use it, such as:
Assets/JavaScript/AI_Normal.js(153,12): BCE0044: expecting ;, found 'i'.
all these answers which aint that helpful… the simplest solution is just to adjust the code slightly
function Split()
{
Destroy(gameObject);
for ( var i : int = 0; i < 10; i ++ )
{
Instantiate( split,transform.position + Vector3(i * 2.0, 0, 2.0), Quaternion.identity );
}
}