Hi, 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: Code (csharp): 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. Any ideas?
Well I destroyed the object, so I would assume not. here is a video of what is happening: Sorry about the music, pandora was playing when I recorded this, so I had to dub over it with youtubes music so I wont get in trouble
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?
They hit each other as you spawn them all at the same position! The problem is in your for-loop where you multiply x coordinate by 0, which means you always get zero!
So, how can I fix that? The script was intended to spread them out, but if I change all 3 coordinates it does nothing.
You could use Random.Range(-3,3) or something like that? To stop the straight line you will need to do it to z as well.
No matter what you do. Your spawn point point is 0,0,2. You are spawning the in the exact same place!!!
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! Code (csharp): 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: Code (csharp): Assets/JavaScript/AI_Normal.js(153,12): BCE0044: expecting ;, found 'i'. Code (csharp): Assets/JavaScript/AI_Normal.js(153,30): BCE0043: Unexpected token: ). Code (csharp): Assets/JavaScript/AI_Normal.js(155,27): BCE0044: expecting :, found '='. Problem is, I checked all of these, and these errors are incorrect. Which leads me to think there is something else wrong with this code.
Generally the console is quite specific with what is wrong in code. If there is an error, it will point to the script in question and the line.
This type of code is admittedly rather foreign to me. I'm not sure how to go about putting it in Js. Help? :3
all these answers which aint that helpful... the simplest solution is just to adjust the code slightly Code (csharp): 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 ); } }
That doesnt help either. They still spawn in a perfectly straight line, extending far away from the origional object :/
/fail That will learn me for not reading what you wanted correctly... thought u were after straight line go with bf games solution then.
I would gladly do that, except I dont know how to convert it into JS. I have 0 experience with C#, only JS, so I dont know where to begin.
Code (csharp): var size = 4; //Just set it to a number that fit the size of your objects for (var i = 0; i < 10; i++) { //convert to radians var angle = 36 * i; var rad = angle * Mathf.PI / 180.0F; var x = Mathf.Cos(rad); var z = Mathf.Sin(rad); Instantiate(obj, transform.position + Vector3(x * size, 0, z * size), Quaternion.identity); } Not used to writing in JS - but something along the lines like this.