Search Unity

Discrepancy between Xcode run code and .ipa file run code??

Discussion in 'Editor & General Support' started by yetstay, Apr 14, 2012.

  1. yetstay

    yetstay

    Joined:
    Mar 28, 2012
    Posts:
    5
    Hi, I have a script that shows different behaviours when run on different conditions. This is what I was trying to do initially: http://forum.unity3d.com/threads/129540-3D-camera-relatively-using-gyroscope-a-la-N.O.V.A.2?highlight=gyro+camera In a nut shell, I was trying to use a gyro to control the camera.

    When I build and run the project on iPhone via Xcode, it works: the camera's pointing at "front" regardless of gyro's rotation and from that point on, depending on the gyro's rotation the camera moves around. However, if I stop the project from running on Xcode(press the stop button), quit the app from running in the background on my iPhone, then start the game again on the phone, the camera's now always thinking that "down" is "front".

    I deployed an independent .ipa file on the phone and the camera still defaults to "down" so I think there might be a logic error but then why does it work when I build it on Xcode for the first time? I suspect Start() is not run properly but I don't know what is happening exactly.

    I've been looking into this on google but I couldn't find any information about this. I though this section of forum would be more suitable to post this as this seems general enough. Any idea why this happens?



    Here's the script if you wanna have a look. You can download the package from above link and apply the script. Thank you for any help!



    Code (csharp):
    1.  
    2.  
    3. // iPhone gyroscope-controlled camera demo v0.3 8/8/11
    4. // Perry Hoberman <hoberman@bway.net>
    5. // Directions: Attach this script to main camera.
    6. // Note: Unity Remote does not currently support gyroscope.
    7.  
    8. // Modified by Sean Lee <seanlee0819@gmail.com> 2012/04/05
    9.  
    10. private var gyroBool : boolean;
    11. private var gyro : Gyroscope;
    12. private var rotFix : Quaternion;
    13.  
    14. private var camParent : GameObject;
    15.  
    16. // quaternion test
    17. public var gyroBody: GameObject;
    18.  
    19. public var CamBody: GameObject;
    20.  
    21. private var camBodyParent : GameObject;
    22.  
    23. private var bodyForward1: Vector3;
    24.  
    25. private var camForward: Vector3;
    26.  
    27. private var inverseGyroRot: Quaternion;
    28.  
    29. private var gyroRot: Quaternion;
    30. private var initialCamParentRot: Quaternion;
    31. private var initialCamRot: Quaternion;
    32.  
    33. private var gamepaused;
    34.  
    35.  
    36. function Start() {
    37.  
    38.     gamepaused = false;
    39.    
    40.     var originalParent = transform.parent; // check if this transform has a parent
    41.     camParent = new GameObject ("camParent"); // make a new parent
    42.     camParent.transform.position = transform.position; // move the new parent to this transform position
    43.     transform.parent = camParent.transform; // make this transform a child of the new parent
    44.     camParent.transform.parent = originalParent; // make the new parent a child of the original parent
    45.        
    46.     gyroBool = SystemInfo.supportsGyroscope; //Input.isGyroAvailable;
    47.    
    48.     if (gyroBool) {
    49.        
    50.         gyro = Input.gyro;
    51.         gyro.enabled = true;
    52.        
    53.         if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
    54.             camParent.transform.eulerAngles = Vector3(90,90,0);
    55.            
    56.         } else if (Screen.orientation == ScreenOrientation.Portrait) {
    57.             camParent.transform.eulerAngles = Vector3(90,180,0);
    58.         }
    59.        
    60.         if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
    61.             rotFix = Quaternion(0,0,0.7071,0.7071);
    62.         } else if (Screen.orientation == ScreenOrientation.Portrait) {
    63.             rotFix = Quaternion(0,0,1,0);
    64.         }
    65.         //Screen.sleepTimeout = 0;
    66.        
    67.         gyroRot = gyro.attitude * rotFix;
    68.        
    69.         gyroBody.transform.localRotation = gyroRot; // where gyro's facing
    70.         //bodyForward1 = gyroBody.transform.forward;
    71.        
    72.        
    73.         //var tempCamForward = Vector3(0, 0, 1);// initial lookat of camera
    74.         //CamBody.transform.forward = tempCamForward;
    75.        
    76.         //if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
    77.         //    CamBody.transform.eulerAngles = Vector3(90, 90, 0);
    78.         //} else if (Screen.orientation == ScreenOrientation.Portrait) {
    79.         //    CamBody.transform.eulerAngles = Vector3(90, 180, 0);
    80.         //}
    81.        
    82.         //initialCamParentRot = CamBody.transform.rotation;
    83.        
    84.         initialCamParentRot = camParent.transform.rotation * Quaternion.EulerRotation(90, 0, 0);
    85.        
    86.         initialCamRot = transform.localRotation; //camParent.transform.FindChild(transform.name).transform.localRotation;
    87.        
    88.         Debug.Log("initial Cam Rot = "+initialCamRot.eulerAngles);
    89.         //camForward = CamBody.transform.forward;
    90.        
    91.         //inverseGyroRot = Quaternion.FromToRotation(bodyForward1, camForward);
    92.         inverseGyroRot = Quaternion.Inverse(gyroBody.transform.rotation);
    93.        
    94.         //Debug.Log("bodyForward1 = "+bodyForward1);
    95.         //Debug.Log("camForward = " + camForward);
    96.         Debug.Log("inverseGyroRot euler angle= "+inverseGyroRot.eulerAngles);
    97.         //Debug.Log("gyro.attitude= "+gyro.attitude);
    98.     }
    99.    
    100.     else {
    101.    
    102.         var originalCamBodyParent = CamBody.transform.parent; // check if this transform has a parent
    103.         camBodyParent = new GameObject ("camBodyParent"); // make a new parent
    104.         camBodyParent.transform.position = CamBody.transform.position; // move the new parent to this transform position
    105.         CamBody.transform.parent = camBodyParent.transform; // make this transform a child of the new parent
    106.         camBodyParent.transform.parent = originalCamBodyParent; // make the new parent a child of the original parent
    107.    
    108.         print("NO GYRO");
    109.        
    110.         initialCamParentRot = camBodyParent.transform.rotation;
    111.         initialCamRot = CamBody.transform.localRotation;
    112.        
    113.         inverseGyroRot = Quaternion.Inverse(gyroBody.transform.rotation);
    114.         Debug.Log("inverseGyroRot in euler angles= " + inverseGyroRot.eulerAngles);
    115.  
    116.     }
    117. }
    118.  
    119. function Update () {
    120.  
    121.     // the following is for pausing in simulator
    122.     if(Input.GetKeyDown("p")){
    123.         if(!gamepaused){
    124.             gamepaused = true;
    125.             Debug.Log("game paused!!");
    126.            
    127.             initialCamParentRot = camParent.transform.rotation;
    128.             initialCamRot = CamBody.transform.localRotation;
    129.  
    130.         }
    131.         else{
    132.             gamepaused = false;
    133.             Debug.Log("game resumed");
    134.  
    135.             inverseGyroRot = Quaternion.Inverse(gyroBody.transform.rotation);
    136.         }  
    137.     }
    138.  
    139.     // handle the camera's transform
    140.     if (gyroBool) {
    141.  
    142.         if(!gamepaused){
    143.        
    144.             gyroRot = gyro.attitude* rotFix;
    145.    
    146.             transform.localRotation = initialCamRot * initialCamParentRot * (inverseGyroRot * gyroRot) ;
    147.            
    148.             //gyroBody.transform.rotation = gyroRot; // where gyro's facing
    149.             //bodyForward1 = gyroBody.transform.forward;
    150.             //print("bodyForward1 = " + bodyForward1);
    151.    
    152.         }
    153.     }
    154.     else
    155.     {
    156.         if(!gamepaused){
    157.        
    158.             var tempRot3 = gyroBody.transform.rotation;
    159.             CamBody.transform.localRotation =  initialCamRot * initialCamParentRot * ( inverseGyroRot * tempRot3 );
    160.         }
    161.     }
    162. }
    163.  
    164.  
    165. function OnApplicationPause(gameInterrupted: boolean){
    166.  
    167.     //var tempGyroOffset:Quaternion;
    168.  
    169.     if(gameInterrupted){
    170.    
    171.         Debug.Log("game paused using the home button");
    172.        
    173.        
    174.         initialCamRot = transform.localRotation * initialCamParentRot; //camParent.transform.FindChild(transform.name).transform.localRotation
    175.         //tempGyroOffset = gyro.attitude * rotFix;
    176.        
    177.         Debug.Log("initial Cam Rot = "+initialCamRot.eulerAngles);
    178.        
    179.         //Time.timeScale = 0.0f; // pause the game so the gyro rotation won't matter
    180.        
    181.         this.gamepaused = true;
    182.        
    183.     }
    184.     else{
    185.    
    186.         Debug.Log("game resumed from home screen!!!");
    187.  
    188.         gyroRot = gyro.attitude * rotFix;
    189.        
    190.         gyroBody.transform.localRotation = gyroRot; // where gyro's facing
    191.         //bodyForward1 = gyroBody.transform.forward;
    192.        
    193.         //inverseGyroRot = Quaternion.FromToRotation(bodyForward1, savedCamForward);
    194.        
    195.         inverseGyroRot = Quaternion.Inverse(gyroBody.transform.rotation);
    196.        
    197.         //Time.timeScale = 1.0f; //  resume the game
    198.        
    199.         //this.gamepaused = false;
    200.        
    201.        
    202.     }
    203.    
    204.     Debug.Log("game paused bool = "+ this.gamepaused);
    205.     Debug.Log("game interrupted bool = "+ gameInterrupted);
    206.        
    207.  
    208. }
    209.  
    210. function OnGUI(){
    211.  
    212.     if(GUI.Button(Rect(10, 10, 150, 150), "pause"))
    213.         pauseCam();
    214.    
    215. }
    216.  
    217.  
    218. function pauseCam(){
    219.  
    220.     if(!gamepaused){
    221.    
    222.         Debug.Log("game paused using the GUI button");
    223.         this.gamepaused = true;
    224.         Debug.Log("game paused bool = "+ this.gamepaused);
    225.        
    226.         initialCamRot = transform.localRotation * initialCamParentRot; //camParent.transform.FindChild(transform.name).transform.localRotation
    227.        
    228.         Debug.Log("initial Cam Rot = "+initialCamRot.eulerAngles);
    229.        
    230.         //Time.timeScale = 0.0f; // pause the game so the gyro rotation won't matter
    231.        
    232.     }
    233.     else{
    234.    
    235.         Debug.Log("game resumed!!!");
    236.         this.gamepaused = false;
    237.        
    238.         Debug.Log("game paused bool = "+ this.gamepaused);
    239.        
    240.        
    241.        
    242.         gyroRot = gyro.attitude * rotFix;
    243.        
    244.         gyroBody.transform.localRotation = gyroRot; // where gyro's facing
    245.         //bodyForward1 = gyroBody.transform.forward;
    246.        
    247.         //inverseGyroRot = Quaternion.FromToRotation(bodyForward1, savedCamForward);
    248.        
    249.         inverseGyroRot = Quaternion.Inverse(gyroBody.transform.rotation);
    250.        
    251.         //Time.timeScale = 1.0f; //  resume the game
    252.        
    253.        
    254.     }
    255.  
    256. }
    257.  
    258.  
    259.