Search Unity

problem with rotation of the object that created in real time ...help me plss :(

Discussion in 'Scripting' started by fandiwhuang, Aug 30, 2011.

  1. fandiwhuang

    fandiwhuang

    Joined:
    Jul 24, 2011
    Posts:
    38
    $Untitled-1.jpg

    Code (csharp):
    1.  
    2.  
    3. var aBox:GameObject;
    4. var bBox:GameObject;
    5. var cBox:GameObject;
    6. var dBox:GameObject;
    7. var eBox:GameObject;
    8. var fBox:GameObject;
    9. var gBox:GameObject;
    10. var hBox:GameObject;
    11. var iBox:GameObject;
    12. var jBox:GameObject;
    13. var kBox:GameObject;
    14. var lBox:GameObject;
    15. var mBox:GameObject;
    16. var nBox:GameObject;
    17. var oBox:GameObject;
    18. var pBox:GameObject;
    19. var qBox:GameObject;
    20. var rBox:GameObject;
    21. var sBox:GameObject;
    22. var tBox:GameObject;
    23. var uBox:GameObject;
    24. var vBox:GameObject;
    25. var wBox:GameObject;
    26. var xBox:GameObject;
    27. var yBox:GameObject;
    28. var zBox:GameObject;
    29.  
    30.  
    31. var aBank:GameObject;
    32. var bBank:GameObject;
    33. var cBank:GameObject;
    34. var dBank:GameObject;
    35. var eBank:GameObject;
    36. var fBank:GameObject;
    37. var gBank:GameObject;
    38. var hBank:GameObject;
    39. var iBank:GameObject;
    40. var jBank:GameObject;
    41. var kBank:GameObject;
    42. var lBank:GameObject;
    43. var mBank:GameObject;
    44. var nBank:GameObject;
    45. var oBank:GameObject;
    46. var pBank:GameObject;
    47. var qBank:GameObject;
    48. var rBank:GameObject;
    49. var sBank:GameObject;
    50. var tBank:GameObject;
    51. var uBank:GameObject;
    52. var vBank:GameObject;
    53. var wBank:GameObject;
    54. var xBank:GameObject;
    55. var yBank:GameObject;
    56. var zBank:GameObject;
    57.  
    58. private var letterBox:Array;
    59. private var banker:Array;
    60. private var numberOfLetter:int;
    61.  
    62. function Start()
    63. {
    64.     numberOfLetter = Random.Range(3,15);
    65.     letterBox = new Array(numberOfLetter);
    66.     banker = new Array(numberOfLetter);
    67.     Debug.Log(numberOfLetter);
    68.    
    69.     for(var i:int ; i < numberOfLetter ;i++)
    70.     {
    71.         var randomPick:int;
    72.         randomPick = Random.Range(1,26);
    73.         if(randomPick ==1)
    74.         {
    75.             //letterBox[i] = aBox;
    76.             letterBox[i] = Instantiate(aBox,Vector3(Random.Range(12,34.5),5,
    77.             Random.Range(33,78)),gameObject.transform.rotation);
    78.             banker[i] = Instantiate(aBank,Vector3(Random.Range(12,34.5),0,
    79.             Random.Range(33,78)),Quaternion.identity);
    80.            
    81.         }
    82.         else if(randomPick == 2)
    83.         {
    84.             //letterBox[i] = bBox;
    85.             letterBox[i] = Instantiate(bBox,Vector3(Random.Range(13.5,35),0,
    86.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    87.             banker[i] = Instantiate(bBank,Vector3(Random.Range(12,34.5),0,
    88.             Random.Range(33,78)),Quaternion.identity);
    89.         }
    90.         else if(randomPick == 3)
    91.         {
    92.             //letterBox[i] = cBox;
    93.             letterBox[i] = Instantiate(cBox,Vector3(Random.Range(13.5,35),0,
    94.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    95.             banker[i] = Instantiate(cBank,Vector3(Random.Range(12,34.5),0,
    96.             Random.Range(33,78)),Quaternion.identity);
    97.         }
    98.         else if(randomPick == 4)
    99.         {
    100.             //letterBox[i] = dBox;
    101.             letterBox[i] = Instantiate(dBox,Vector3(Random.Range(13.5,35),0,
    102.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    103.             banker[i] = Instantiate(dBank,Vector3(Random.Range(12,34.5),0,
    104.             Random.Range(33,78)),Quaternion.identity);
    105.         }
    106.         else if(randomPick == 5)
    107.         {
    108.             //letterBox[i] = eBox;
    109.             letterBox[i] = Instantiate(eBox,Vector3(Random.Range(13.5,35),0,
    110.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    111.             banker[i] = Instantiate(eBank,Vector3(Random.Range(12,34.5),0,
    112.             Random.Range(33,78)),Quaternion.identity);
    113.         }
    114.         else if(randomPick == 6)
    115.         {
    116.             //letterBox[i] = fBox;
    117.             letterBox[i] = Instantiate(fBox,Vector3(Random.Range(13.5,35),0,
    118.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    119.             banker[i] = Instantiate(fBank,Vector3(Random.Range(12,34.5),0,
    120.             Random.Range(33,78)),Quaternion.identity);
    121.         }
    122.         else if(randomPick == 7)
    123.         {
    124.             //letterBox[i] = gBox;
    125.             letterBox[i] = Instantiate(gBox,Vector3(Random.Range(13.5,35),0,
    126.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    127.             banker[i] = Instantiate(gBank,Vector3(Random.Range(12,34.5),0,
    128.             Random.Range(33,78)),Quaternion.identity);
    129.         }
    130.         else if(randomPick == 8)
    131.         {
    132.             //letterBox[i] = hBox;
    133.             letterBox[i] = Instantiate(hBox,Vector3(Random.Range(13.5,35),0,
    134.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    135.             banker[i] = Instantiate(hBank,Vector3(Random.Range(12,34.5),0,
    136.             Random.Range(33,78)),Quaternion.identity);
    137.         }
    138.         else if(randomPick == 9)
    139.         {
    140.             //letterBox[i] = iBox;
    141.             letterBox[i] = Instantiate(iBox,Vector3(Random.Range(13.5,35),0,
    142.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    143.             banker[i] = Instantiate(iBank,Vector3(Random.Range(12,34.5),0,
    144.             Random.Range(33,78)),Quaternion.identity);
    145.         }
    146.         else if(randomPick == 10)
    147.         {
    148.             //letterBox[i] = jBox;
    149.             letterBox[i] = Instantiate(jBox,Vector3(Random.Range(13.5,35),0,
    150.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    151.             banker[i] = Instantiate(jBank,Vector3(Random.Range(12,34.5),0,
    152.             Random.Range(33,78)),Quaternion.identity);
    153.         }
    154.         else if(randomPick == 11)
    155.         {
    156.             //letterBox[i] = kBox;
    157.             letterBox[i] = Instantiate(kBox,Vector3(Random.Range(13.5,35),0,
    158.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    159.             banker[i] = Instantiate(kBank,Vector3(Random.Range(12,34.5),0,
    160.             Random.Range(33,78)),Quaternion.identity);
    161.         }
    162.         else if(randomPick == 12)
    163.         {
    164.             //letterBox[i] = lBox;
    165.             letterBox[i] = Instantiate(lBox,Vector3(Random.Range(13.5,35),0,
    166.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    167.             banker[i] = Instantiate(lBank,Vector3(Random.Range(12,34.5),0,
    168.             Random.Range(33,78)),Quaternion.identity);
    169.         }
    170.         else if(randomPick == 13)
    171.         {
    172.             //letterBox[i] = mBox;
    173.             letterBox[i] = Instantiate(mBox,Vector3(Random.Range(13.5,35),0,
    174.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    175.             banker[i] = Instantiate(mBank,Vector3(Random.Range(12,34.5),0,
    176.             Random.Range(33,78)),Quaternion.identity);
    177.         }
    178.         else if(randomPick == 14)
    179.         {
    180.             //letterBox[i] = nBox;
    181.             letterBox[i] = Instantiate(nBox,Vector3(Random.Range(13.5,35),0,
    182.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    183.             banker[i] = Instantiate(nBank,Vector3(Random.Range(12,34.5),0,
    184.             Random.Range(33,78)),Quaternion.identity);
    185.         }
    186.         else if(randomPick == 15)
    187.         {
    188.             //letterBox[i] = oBox;
    189.             letterBox[i] = Instantiate(oBox,Vector3(Random.Range(13.5,35),0,
    190.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    191.             banker[i] = Instantiate(oBank,Vector3(Random.Range(12,34.5),0,
    192.             Random.Range(33,78)),Quaternion.identity);
    193.         }
    194.         else if(randomPick == 16)
    195.         {
    196.             //letterBox[i] = pBox;
    197.             letterBox[i] = Instantiate(pBox,Vector3(Random.Range(13.5,35),0,
    198.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    199.             banker[i] = Instantiate(pBank,Vector3(Random.Range(12,34.5),0,
    200.             Random.Range(33,78)),Quaternion.identity);
    201.         }
    202.         else if(randomPick == 17)
    203.         {
    204.             //letterBox[i] = qBox;
    205.             letterBox[i] = Instantiate(qBox,Vector3(Random.Range(13.5,35),0,
    206.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    207.             banker[i] = Instantiate(qBank,Vector3(Random.Range(12,34.5),0,
    208.             Random.Range(33,78)),Quaternion.identity);
    209.         }
    210.         else if(randomPick == 18)
    211.         {
    212.             //letterBox[i] = rBox;
    213.             letterBox[i] = Instantiate(rBox,Vector3(Random.Range(13.5,35),0,
    214.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    215.             banker[i] = Instantiate(rBank,Vector3(Random.Range(12,34.5),0,
    216.             Random.Range(33,78)),Quaternion.identity);
    217.         }
    218.         else if(randomPick == 19)
    219.         {
    220.             //letterBox[i] =sBox;
    221.             letterBox[i] = Instantiate(sBox,Vector3(Random.Range(13.5,35),0,
    222.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    223.             banker[i] = Instantiate(sBank,Vector3(Random.Range(12,34.5),0,
    224.             Random.Range(33,78)),Quaternion.identity);
    225.         }
    226.         else if(randomPick == 20)
    227.         {
    228.             //letterBox[i] = tBox;
    229.             letterBox[i] = Instantiate(tBox,Vector3(Random.Range(13.5,35),0,
    230.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    231.             banker[i] = Instantiate(tBank,Vector3(Random.Range(12,34.5),0,
    232.             Random.Range(33,78)),Quaternion.identity);
    233.         }
    234.         else if(randomPick == 21)
    235.         {
    236.             //letterBox[i] = uBox;
    237.             letterBox[i] = Instantiate(uBox,Vector3(Random.Range(13.5,35),0,
    238.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    239.             banker[i] = Instantiate(uBank,Vector3(Random.Range(12,34.5),0,
    240.             Random.Range(33,78)),Quaternion.identity);
    241.         }
    242.         else if(randomPick == 22)
    243.         {
    244.             //letterBox[i] = vBox;
    245.             letterBox[i] = Instantiate(vBox,Vector3(Random.Range(13.5,35),0,
    246.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    247.             banker[i] = Instantiate(vBank,Vector3(Random.Range(12,34.5),0,
    248.             Random.Range(33,78)),Quaternion.identity);
    249.         }
    250.         else if(randomPick == 23)
    251.         {
    252.             //letterBox[i] = wBox;
    253.             letterBox[i] = Instantiate(wBox,Vector3(Random.Range(13.5,35),0,
    254.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    255.             banker[i] = Instantiate(wBank,Vector3(Random.Range(12,34.5),0,
    256.             Random.Range(33,78)),Quaternion.identity);
    257.         }
    258.         else if(randomPick == 24)
    259.         {
    260.             //letterBox[i] = xBox;
    261.             letterBox[i] = Instantiate(xBox,Vector3(Random.Range(13.5,35),0,
    262.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    263.             banker[i] = Instantiate(xBank,Vector3(Random.Range(12,34.5),0,
    264.             Random.Range(33,78)),Quaternion.identity);
    265.         }
    266.         else if(randomPick == 25)
    267.         {
    268.             //letterBox[i] = yBox;
    269.             letterBox[i] = Instantiate(yBox,Vector3(Random.Range(13.5,35),0,
    270.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    271.             banker[i] = Instantiate(yBank,Vector3(Random.Range(12,34.5),0,
    272.             Random.Range(33,78)),Quaternion.identity);
    273.         }
    274.         else
    275.         {
    276.             //letterBox[i] = zBox;
    277.             letterBox[i] = Instantiate(zBox,Vector3(Random.Range(13.5,35),0,
    278.             Random.Range(31.5,80.7)),gameObject.transform.rotation);
    279.             banker[i] = Instantiate(zBank,Vector3(Random.Range(12,34.5),0,
    280.             Random.Range(33,78)),Quaternion.identity);
    281.         }
    282.     }
    283. }
    284.  
    285. function Update ()
    286. {
    287.    
    288. }
    289.  
    290.  
    this is my code and i attached it on my game stage and my problem is this script is supposed to create an object with a random position with NO rotation...

    but somehow my objects are always rotating when its created as u can see in the picture ..

    i hv tried to fix it but seems its fruitless :(

    i hv tried
    - add an rigidbody and constraint rotation
    - add another code under existing for statement
    Code (csharp):
    1.  
    2.  
    3. for(var i:int;i< numberOfLetter ;i++)
    4. {
    5. banker[i].transform.position.x = 270;
    6. }
    7.  
    8.  
    - add additional code for every single prefab

    Code (csharp):
    1.  
    2.  
    3. gameObject.transform.rotation.x = 270;
    4.  
    5.  

    none of these is working ...
    im noob at unity and programming plsss help me i dont know what ive done wrong :(
     
  2. RoiDanielsen

    RoiDanielsen

    Joined:
    Nov 10, 2010
    Posts:
    130
    Im a newbie also, but maybe the objects that are spawned in real time are really 0 rotation, while the other you have rotated when you placed it?
     
  3. fandiwhuang

    fandiwhuang

    Joined:
    Jul 24, 2011
    Posts:
    38
    its become x=0 y=0 z=0 when they are spawned but the prefab has 270 x rotation and i didnt change anything , it has been like this when i imported it from 3d software ...

    and also i have added additional script that should make every object has a 270 x rotation ..but somehow that doesnt work

    :(
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    OK, first things first, lets get your code to something a lot more manageable.

    Code (csharp):
    1.  
    2. var letters : GameObject[];
    3. var banks : GameObject[];
    4. private var letterArray : Array;
    5. private var bankArray : Array;
    6. private var numberOfLetter:int;
    7.  
    8. function Start(){
    9.     if(letters.Length != banks.Length) return;
    10.     numberOfLetter = Random.Range(3,15);
    11.     letterArray = new Array();
    12.     bankArray = new Array();
    13.     Debug.Log();
    14.     for(var i : int=0; i< numberOfLetter){
    15.         var randomPick:int;
    16.         randomPick = Random.Range(1,letters.Length);
    17.         var letter : GameObject = Instantiate(letters[randomPick],Vector3(Random.Range(12,34.5),5,
    18.             Random.Range(33,78)),gameObject.transform.rotation);
    19.         var bank : GameObject = Instantiate(banks[randomPick],Vector3(Random.Range(12,34.5),0,
    20.             Random.Range(33,78)),Quaternion.identity);
    21.         letterArray.Add(letter);
    22.         bankArray.Add(bank);
    23.     }
    24. }
    25.  
    26. function Update ()
    27. {
    28.    
    29. }
    30.  
    So instead of making 500 variables, we make 2 arrays. The cool thing is that these can be any size arrays, so if you wanted to use numbers or whatever in there, it will work.

    Now, onto your problem. In the 3d program that you created these objects in, they must be up facing objects. If not, then they will be facing whatever facing that you have in the 3d program in Unity.

    To fix this, you do not have to recreate anything, but put each object on an empty game object, and make sure it is facing the right way. Make prefabs of them to save them that way.
     
    Last edited: Aug 30, 2011
  5. fandiwhuang

    fandiwhuang

    Joined:
    Jul 24, 2011
    Posts:
    38

    how to make sure its facing the right way? and how it should be look like ?
    i mean currently if i drag my object to the scene , i will get exactly what i want with the right rotation without changing anything..doesnt that mean i have already had a gameobject that facing the right way ???

    and another question

    im interested with the way u make my code shorter but i dont quite understand if i dont declare all the game objects im gonna assign to my array , where i could get the information of my prefab then ?? i noticed that your code only has 2 var with GameObject type ... sorry im just curious and im still new at unity and programming thing but i would more than happy if u can explain to me :)
     
    Last edited: Aug 30, 2011
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    When you instantiate something, you are doing with Quaternion.identity. This is exactly as if you had drug it from the project. This is why I questioned if the objects were orientated right.

    LOL, and now that I look at it I see it....
    Code (csharp):
    1.  
    2. var letter : GameObject = Instantiate(letters[randomPick],Vector3(Random.Range(12,34.5),5,
    3.             Random.Range(33,78)),[COLOR="red"]gameObject.transform.rotation[/COLOR]);
    4.  
    OK, in the code I did, I used two GameObject array's to handle the letters and banks that you had. I did this so we wouldn't have to write 500 lines of code to manage them. ;) the [] on the end of the variable type defines it as an array. This is not to be confused with the Array object used elsewhere in the script. They are different.

    Lets look at as an object standpoint. Each object you drag into the Letters Array is a prefab or object in the game. They have an id which identifies where they come from and what they are.

    As we go thorough the Start function, we Instantiate those objects, then add them to the array "letterArray" and "bankArray". (speaking of this, I had to go back into the code and label the globals correctly. my bad) This means, that they are now physical game objects in the game and are ready to use.

    The point of not defining a static length array is so that we could just Add that many components to the array. We could have just as easily defined letterArray and bankArray as GameObject[] and defined each object there. Probably would have saved a nanosecond or two in the process but is not necessary. They are all still accessed as letterArray[index] and so on.
     
  7. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,768
    transform.rotation is a Quaternion so doing transform.rotation.x = 270.0f; wont do what you expect try transform.rotation.eulerAngles.x = 270.0f;
    Or more completely transform.rotation = Quaternion.Euler(270.0f, 0.0f, 0.0f);
     
    Last edited: Aug 30, 2011
  8. fandiwhuang

    fandiwhuang

    Joined:
    Jul 24, 2011
    Posts:
    38
    thx man for explaining that to me . that will helps me manage my code more easily :D
     
  9. fandiwhuang

    fandiwhuang

    Joined:
    Jul 24, 2011
    Posts:
    38
    that works !!!! Thank youuu so muchhhhhh !!!!! :):):):):):):):):):):):):):):):):):):):):)