Search Unity

Enemies flying away :(

Discussion in 'Scripting' started by Sajid, Feb 3, 2013.

  1. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    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):
    1. function Split()
    2. {  
    3.     Destroy(gameObject);
    4.     for ( var i : int = 0; i < 10; i ++ )
    5.     {
    6.        Instantiate( split,transform.position + Vector3(i * 0, 0, 2.0), Quaternion.identity );  
    7.     }
    8. }
    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?
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Is the original enemy's collider still active?
     
  3. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    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 :p
     
  4. Kaze_Senshi

    Kaze_Senshi

    Joined:
    Feb 19, 2012
    Posts:
    243
    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?
     
  5. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    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!
     
  6. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    So, how can I fix that? The script was intended to spread them out, but if I change all 3 coordinates it does nothing.
     
  7. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    But you don't spread them out, you instantiate them all at the exact same position!
     
  8. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    You use i * 0 for your x co ordinate.. So x, pos, will always be 0.
     
  9. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    If I do that they all spawn in a straight line D:
     
  10. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    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.
     
  11. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    even if I do Z or even Y, it doesnt work D:
     
  12. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    No matter what you do. Your spawn point point is 0,0,2. You are spawning the in the exact same place!!!
     
    Last edited: Feb 3, 2013
  13. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    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):
    1.  
    2.             int size = 4; //Just set it to a number that fit the size of your objects
    3.            
    4.             for (int i = 0; i < 10; i++)
    5.             {
    6.                 //convert to radians
    7.                 float angle = 36 * i;
    8.                 float rad = angle * Mathf.PI / 180.0F;
    9.  
    10.                 float x = Mathf.Cos(rad);
    11.                 float z = Mathf.Sin(rad);
    12.                
    13.                 Instantiate(obj, transform.position + new Vector3(x * size, 0, z * size), Quaternion.identity);
    14.             }
    15.  
     
  14. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    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):
    1. Assets/JavaScript/AI_Normal.js(153,12): BCE0044: expecting ;, found 'i'.
    Code (csharp):
    1. Assets/JavaScript/AI_Normal.js(153,30): BCE0043: Unexpected token: ).
    Code (csharp):
    1. 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.
     
  15. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    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.
     
  16. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    It is C#! And i did test it and had no errors. So you should just re-write it in js ;)
     
  17. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    This type of code is admittedly rather foreign to me. I'm not sure how to go about putting it in Js. Help? :3
     
  18. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    all these answers which aint that helpful... the simplest solution is just to adjust the code slightly

    Code (csharp):
    1.  
    2.  
    3. function Split()
    4. {  
    5.     Destroy(gameObject);
    6.     for ( var i : int = 0; i < 10; i ++ )
    7.     {
    8.        Instantiate( split,transform.position + Vector3(i * 2.0, 0, 2.0), Quaternion.identity );  
    9.     }
    10. }
    11.  
    12.  
     
  19. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    That doesnt help either. They still spawn in a perfectly straight line, extending far away from the origional object :/
     
  20. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    /fail

    That will learn me for not reading what you wanted correctly... thought u were after straight line

    go with bf games solution then.
     
  21. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199

    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.
     
  22. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Code (csharp):
    1.  
    2.         var size = 4; //Just set it to a number that fit the size of your objects
    3.  
    4.            
    5.  
    6.             for (var i = 0; i < 10; i++)
    7.  
    8.             {
    9.  
    10.                 //convert to radians
    11.  
    12.                 var angle = 36 * i;
    13.  
    14.                 var rad = angle * Mathf.PI / 180.0F;
    15.  
    16.  
    17.                 var x = Mathf.Cos(rad);
    18.  
    19.                 var z = Mathf.Sin(rad);
    20.            
    21.  
    22.                 Instantiate(obj, transform.position + Vector3(x * size, 0, z * size), Quaternion.identity);
    23.  
    24.             }
    25.  
    26.  
    Not used to writing in JS - but something along the lines like this.
     
  23. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    That worked perfectly. Thanks a lot!

    This site needs a rep system.
     
  24. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Glad to help.