Search Unity

Reset Accelerometer? Is it possible?

Discussion in 'iOS and tvOS' started by sbuchbinder, Nov 7, 2008.

  1. sbuchbinder

    sbuchbinder

    Joined:
    Apr 4, 2008
    Posts:
    53
    Is it possible to access and modify the accelerometer settings from Unity? I just want the user to be able to set up their configuration (based on how they hold the iphone) before the game starts, but don't know if it's possible to actually reset the accelerometer from unity.
     
  2. sbuchbinder

    sbuchbinder

    Joined:
    Apr 4, 2008
    Posts:
    53
    Just a quick follow up.

    A good example of what I want to accomplish is in the 'Settings' of the iPhone game 'Labyrinth'.
     
  3. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Posts:
    552
    There is no "Setting" for doing this. What all games do that have this option is sample the accelerometer when the player asks to calibrate. The game then applies that sample as an offset to all input which then becomes the input that it uses for gameplay.

    The accelerometer will always point in the direction of acceleration.
     
  4. sbuchbinder

    sbuchbinder

    Joined:
    Apr 4, 2008
    Posts:
    53
    Ok, that makes sense. But will the accelerometer read past it's limits (-1 to 1).

    For instance if I calibrate in game at say a 45 degree angle (on x axis) so that that's my center. Once I tilt it to 90 degrees the iphone accelerometer would recognize that as 1, whereas in game would recognize that as being 0.5. If I continue tilting, will it give a value greater than 1?
     
  5. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    You simply need to make your own internal representation of the behavior you want and map the accelerometer onto it.
     
  6. sbuchbinder

    sbuchbinder

    Joined:
    Apr 4, 2008
    Posts:
    53
    ok cool...thanks a bunch to both of you.
     
  7. cheezorg

    cheezorg

    Joined:
    Jun 5, 2008
    Posts:
    394
    I'm also interested in this but not quite sure where to set something like this.

    Using Star Trooper (which is awesome btw) as an example, the iPhone needs to be flat to read zero. To change this to 45 degrees, as sbuchbinder mentioned, is this something you would change in PlayerControls.js?

    - Or does this require a new script entirely?


    Here is the default PlayerControls.js script for reference:
    Code (csharp):
    1.  
    2. var turnSpeed : float = 10.0;
    3. var maxTurnLean : float = 50.0;
    4. var maxTilt : float = 50.0;
    5.  
    6. var sensitivity : float = 10.0;
    7.  
    8. var forwardForce : float = 1.0;
    9. var guiSpeedElement : Transform;
    10.  
    11. private var normalizedSpeed : float = 0.2;
    12. private var euler : Vector3 = Vector3.zero;
    13.  
    14. var horizontalOrientation : boolean = true;
    15.  
    16. function Awake () {
    17.     if (horizontalOrientation)
    18.         Screen.SetResolution(480, 320, true);
    19.     else
    20.         Screen.SetResolution(320, 480, true);
    21.  
    22.     guiSpeedElement.position = new Vector3 (0, normalizedSpeed, 0);
    23. }
    24.  
    25. function Update () {
    26.     for (var evt : iPhoneTouch in iPhoneInput.touches)
    27.     {
    28.         if (evt.phase == iPhoneTouchPhase.Moved)
    29.         {
    30.             normalizedSpeed = evt.position.y / Screen.height;
    31.             guiSpeedElement.position = new Vector3 (0, normalizedSpeed, 0);
    32.         }
    33.     }
    34. }
    35.  
    36.  
    37. function FixedUpdate () {
    38.     rigidbody.AddRelativeForce(0, 0, normalizedSpeed * forwardForce);
    39.    
    40.     var accelerator : Vector3 = iPhoneInput.acceleration;
    41.  
    42.     if (horizontalOrientation)
    43.     {
    44.         var t : float = accelerator.x;
    45.         accelerator.x = -accelerator.y;
    46.         accelerator.y = t;
    47.     }
    48.    
    49.     // Rotate turn based on acceleration       
    50.     euler.y += accelerator.x * turnSpeed;
    51.     // Since we set absolute lean position, do some extra smoothing on it
    52.     euler.z = Mathf.Lerp(euler.z, -accelerator.x * maxTurnLean, 0.2);
    53.  
    54.     // Since we set absolute lean position, do some extra smoothing on it
    55.     euler.x = Mathf.Lerp(euler.x, accelerator.y * maxTilt, 0.2);
    56.    
    57.     // Apply rotation and apply some smoothing
    58.     var rot : Quaternion = Quaternion.Euler(euler);
    59.     transform.rotation = Quaternion.Lerp (transform.rotation, rot, sensitivity);
    60. }
    61.  
    62.  
     
  8. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    I haven't gotten around to it yet, but I was thinking if you could convert the gravity vector to Euler angles, then instead of trying to set everything to '0', you would just store a reference value that you will consider '0' and measure the deviation from that point and the degree of deviation is your input value.

    So for example, if the user is holding the device at 45 degrees about the X-axis, and you want that to be '0', just store 45 degrees as the "neutral" angle, then if it changes to 90 or 0 degrees, you'd subtract your "neutral" angle from that and get how far the device has moved from "neutral".

    If you get this implemented, I'd really appreciate it if you could share your findings in this thread. If I get there first (probably be a few days before I can get to it), I'll post my findings as well.
     
  9. cheezorg

    cheezorg

    Joined:
    Jun 5, 2008
    Posts:
    394
    Sounds good. I'll see if I can get my head around that. :D
     
  10. cheezorg

    cheezorg

    Joined:
    Jun 5, 2008
    Posts:
    394
    After looking at this some more I found an easy way to set "flat" closer to 45 degrees.

    Obviously, user set calibration is the end goal, but in the meantime this allows up and down ship motion from an easier-to-view angle.

    Replace the 'if (horizontalOrientation)' section of the 'function FixedUpdate' in PlayerControls.js with the following:

    Code (csharp):
    1.  
    2. if (horizontalOrientation)
    3.     {
    4.         var t : float = accelerator.x;
    5.         accelerator.x = -accelerator.y;
    6.         accelerator.y = t + 0.7;
    7.     }
    8.  
    9.  
    Adjust the 0.7 to match your desired angle (0.7 feels like 45 degrees to me, give or take).
     
  11. grobm

    grobm

    Joined:
    Aug 15, 2005
    Posts:
    217
    This approach does not give you the full rotation to motion.
     
  12. MadMac

    MadMac

    Joined:
    Dec 30, 2008
    Posts:
    61
    Hello All,
    so well i'm also interested in this but can't get it to work.
    Well i want to learn from Startrooper but i'm not able to change the orientation...
    Well is there a Script for the calibration of the iPhone orientation ?
    Would be great if someone cold help out a little...
    So what i want when Startrooper starts is a Button for calibrating the iPhone and if i press ok the Game starts.
    Many thxxx
    Stefan
     
  13. Akano

    Akano

    Joined:
    Mar 3, 2008
    Posts:
    6
    Hi,

    here is how you get the full up and down rotation:

    Code (csharp):
    1. accelerator.y = (accelerator.y + 0.5) * 2;
    This will set the "flat" zero orientation to exactly 45 degree.

    If you want to reverse it just use a table and use "rule of three" to map the values to the acceleration output. 0 deg = 1; 45 deg = 0; 90 deg = -1

    Hope this will help you to find the way also set other orientations.

    As one note, if you set this orientation, you have to recalculate the turn also a little ;-)
     
  14. MadMac

    MadMac

    Joined:
    Dec 30, 2008
    Posts:
    61
    Thxx Akano !!!
    That's a usefull answer !!!!
    I miss it a lot here in this forums....
    Many keep the secrets for themselfs... ;-)
    Thank you awesome help !
    Stefan
     
  15. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I'm sorry, but that's plain wrong; this is the most helpful forum I've seen. If you don't get an answer, 99% of the time it's because A) it's plainly in the docs, or B) it's been covered many times on the forums already, or C) it's a basic programming question that doesn't really have anything to do with Unity per se, or D) nobody knows.

    --Eric
     
    LuckyMisterSleven likes this.
  16. cheezorg

    cheezorg

    Joined:
    Jun 5, 2008
    Posts:
    394
    @MadMac - Eric said it best.

    @Akano - Nice job on the rotation fix! :D
     
  17. simonre

    simonre

    Joined:
    Jul 12, 2008
    Posts:
    53
    I use...

    Code (csharp):
    1. function FixedUpdate () {
    2.     var dir : Vector3 = Vector3(0, 0, 0);
    3.        
    4.     dir.x = iPhoneInput.acceleration.y;
    5.     dir.y = (iPhoneInput.acceleration.x)-tilt;
    6.     dir.z = shipSpeed;
     
  18. grobm

    grobm

    Joined:
    Aug 15, 2005
    Posts:
    217
    I tested this, it does not work, it simply does a nose dive into the ground.

    Code (csharp):
    1.  
    2. if (horizontalOrientation)
    3.    {
    4.       var t : float = accelerator.x;
    5.       accelerator.x = -accelerator.y;
    6.       accelerator.y = (accelerator.y + 0.5) * 2;
    7.       //accelerator.y = t;
    8.    }
    9.  
    Is this how you intended the code to work?
    I wish Unity would supply an example just on accelerator manipulation since a bunch of people have different theories on how it should work for this example.
     
  19. unitybuzz

    unitybuzz

    Joined:
    Jan 29, 2009
    Posts:
    67
    Me too interested here. So, Akano's answer is the final one :?:
    I've not given try to any yet. All this need to be done in the Game Settings screen, so that the user can adjust the settings as preferable :?:
     
  20. unitybuzz

    unitybuzz

    Joined:
    Jan 29, 2009
    Posts:
    67
    I posted my previous question after watching the responses on the first page only. I just couldn't see the next page then.
     
  21. Sandoze

    Sandoze

    Joined:
    Oct 4, 2008
    Posts:
    19
    Here's how we handled it on our most recent Unity project "Vans Sk8" (Be sure to check out our new trailer at http://www.youtube.com/watch?v=jQ89B5kVcC0)

    Code (csharp):
    1.    
    2. //Used to calibrate the initial iPhoneIput.acceleration input
    3. void calibrateAccelerometer(){
    4.     Vector3 wantedDeadZone = iPhoneInput.acceleration;
    5.     Quaternion rotateQuaternion = Quaternion.FromToRotation(new Vector3(0f, 0f, -1f), wantedDeadZone);
    6.        
    7.     //create identity matrix ... rotate our matrix to match up with down vec
    8.     Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, rotateQuaternion, new Vector3(1f, 1f, 1f));
    9.     //get the inverse of the matrix
    10.     this.calibrationMatrix = matrix.inverse;
    11. }
    12.    
    13. //Whenever you need an accelerator value from the user
    14. //call this function to get the 'calibrated' value
    15. Vector3 getAccelerometer(Vector3 accelerator){
    16.     Vector3 accel = this.calibrationMatrix.MultiplyVector(accelerator);
    17.     return accel;
    18. }
     
  22. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Tho' I've not tested the following code, as it doesn't work for my current set up, in an attempt to understand the previous snippet, I translated it into Unity.js.


    This is to check the translation, in case someone sees a problem:

    Code (csharp):
    1. var calibrationMatrix : Matrix4x4;
    2.  
    3. //Used to calibrate the initial iPhoneIput.acceleration input
    4. function calibrateAccelerometer () {
    5. //void calibrateAccelerometer(){
    6.    var wantedDeadZone : Vector3 = iPhoneInput.acceleration;
    7. //   Vector3 wantedDeadZone = iPhoneInput.acceleration;
    8.    var rotateQuaternion : Quaternion = Quaternion.FromToRotation(new Vector3(0.0, 0.0, -1.0), wantedDeadZone);
    9. //   Quaternion rotateQuaternion = Quaternion.FromToRotation(new Vector3(0f, 0f, -1f), wantedDeadZone);
    10.      
    11.    //create identity matrix ... rotate our matrix to match up with down vec
    12.    var matrix : Matrix4x4 = Matrix4x4.TRS(Vector3.zero, rotateQuaternion, new Vector3(1.0, 1.0, 1.0));
    13. //   Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, rotateQuaternion, new Vector3(1f, 1f, 1f));
    14.    //get the inverse of the matrix
    15.    calibrationMatrix = matrix.inverse;
    16. //   this.calibrationMatrix = matrix.inverse;
    17. }
    18.    
    19. //Whenever you need an accelerator value from the user
    20. //call this function to get the 'calibrated' value
    21. function getAccelerometer (accelerator : Vector3) {
    22. //Vector3 getAccelerometer(Vector3 accelerator){
    23.    var accel : Vector3 = calibrationMatrix.MultiplyVector(accelerator);
    24. //   Vector3 accel = this.calibrationMatrix.MultiplyVector(accelerator);
    25.    return accel;
    26. //   return accel;
    27. }
    and here is the clean code:

    Code (csharp):
    1. var calibrationMatrix : Matrix4x4;
    2.  
    3. //Used to calibrate the initial iPhoneIput.acceleration input
    4. function calibrateAccelerometer () {
    5.    var wantedDeadZone : Vector3 = iPhoneInput.acceleration;
    6.    var rotateQuaternion : Quaternion = Quaternion.FromToRotation(new Vector3(0.0, 0.0, -1.0), wantedDeadZone);
    7.      
    8.    //create identity matrix ... rotate our matrix to match up with down vec
    9.    var matrix : Matrix4x4 = Matrix4x4.TRS(Vector3.zero, rotateQuaternion, new Vector3(1.0, 1.0, 1.0));
    10.    
    11.    //get the inverse of the matrix
    12.    calibrationMatrix = matrix.inverse;
    13. }
    14.    
    15. //Whenever you need an accelerator value from the user
    16. //call this function to get the 'calibrated' value
    17. function getAccelerometer (accelerator : Vector3) {
    18.    var accel : Vector3 = calibrationMatrix.MultiplyVector(accelerator);
    19.    return accel;
    20. }
    Let em know if there is a problem, and I'll give fix it.
     
  23. cheezorg

    cheezorg

    Joined:
    Jun 5, 2008
    Posts:
    394
    Vans SK8 looks great! Nice!
     
  24. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Just a confirmation post that I've got the aforementioned Unity.js code working in my game.

    Thanks a lot to the folks at FuelGames!
     
  25. Muckel

    Muckel

    Joined:
    Mar 26, 2009
    Posts:
    471
    Thank You very much Little Angel...
    ... your name say it :)
    can you also explain how to implement it the best way in the Trooper Demo?
    thank you all...
    kerstin :oops:
     
  26. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Muckel,

    I've not looked at the Trooper Demo, but I can tell you a little about how I used this code.

    In my game I have a rigid body object that takes the iPhone input (raw thrust) and runs it through an equation to come up with force to move the object (thrust).

    Originally, I used an equation that was based around in input from the iPhone accelerometer that kept the phone <about> 45*. The equation made the final thrust value a negative number with any input from the iPhone less that 0.7, and all input from the iPhone over 0.85 would be greater than 1.0, then I clamped it with an if() that anything less/equal to 0.0 was 0.0 and anything greater than 1.0 was 1.0.

    Cumbersome.

    Then I gave the User an option to modify this in Player Prefs, with a generic "flatter" "taller" slider.

    Bulky.

    With this code from FuelGames, I had to re-jigger my thrust equation to be based on 0.0 (rather than 0.7), as THEIR code adjusts where YOUR code will find a 0.0 reading from the iPhone.

    Their code will put 0.0 at the angle of tilt that the user has selected when they "calibrate".

    To work for me I simply added their code to my Player/Ship Controller script:

    Code (csharp):
    1. //Used to calibrate the initial iPhoneIput.acceleration input
    2. function CalibrateAccelerometer () {
    3.     var accelerationSnapshot : Vector3 = iPhoneInput.acceleration;
    4.     var rotateQuaternion : Quaternion = Quaternion.FromToRotation(new Vector3(0.0, 0.0, -1.0), accelerationSnapshot);
    5.  
    6.     //create identity matrix ... rotate our matrix to match up with down vec
    7.     var matrix : Matrix4x4 = Matrix4x4.TRS(Vector3.zero, rotateQuaternion, new Vector3(1.0, 1.0, 1.0));
    8.  
    9.     //get the inverse of the matrix
    10.     calibrationMatrix = matrix.inverse;
    11.     GameStatus.calibrationMatrix = matrix.inverse;
    12. }
    13.  
    14.  
    15.  
    16. //  Get the 'calibrated' value from the iPhone Input
    17. function FixAcceleration (accelerator : Vector3) {
    18.     var fixedAcceleration : Vector3 = calibrationMatrix.MultiplyVector(accelerator);
    19.     return fixedAcceleration;
    20. }
    As you can see, I've renamed some of the functions so I can remember them, but this is that same code.

    I also save it to a global file with the line:

    Code (csharp):
    1.     GameStatus.calibrationMatrix = matrix.inverse;
    Make sure at the top of your script where you are setting up your variables you add:

    Code (csharp):
    1. private var calibrationMatrix : Matrix4x4;
    as this variable is needed OUTSIDE of their functions, and is needed by your Update(); or FixedUpdate (); functions.

    Finally INSIDE Update(); or FixedUpdate (); function <function FixedUpdate() {} for me as this is used on a rigid body in my game> I have the code:

    Code (csharp):
    1. //  Retrieve input from Accelerometer
    2.     var acceleration : Vector3 = iPhoneInput.acceleration;
    3.     var fixedAcceleration : Vector3 = FixAcceleration (acceleration);
    4.     var rawThrust : float = (fixedAcceleration.y * -1.0);
    5.     var rawTurn : float = acceleration.x;  
    6.     var turn : float = 0.0;
    7.     var thrust = (rawThrust * thrustEquation);
    8.    
    9.     if (thrust < 0)
    10.         thrust = 0;
    11.        
    12.     if (thrust > 1)
    13.         thrust = 1;
    14.  
    15.     if (Mathf.Abs(rawTurn) > turnThreshold) {
    16.         turn = rawTurn;
    17.        
    18.         if (turn < -1.0)
    19.             turn = -1.0;
    20.            
    21.         if (turn > 1.0)
    22.             turn = 1.0;
    23.     }
    The line of my code that used THEIR code is:

    Code (csharp):
    1.     var fixedAcceleration : Vector3 = FixAcceleration (acceleration);
    This feeds the input from your iPhone through their function and sets it so the user defined tilt is 0.0. Then you use fixedAcceleration as if it were the iPhoneInput.acceleration in your script.

    Does this answer your question?
     
  27. Muckel

    Muckel

    Joined:
    Mar 26, 2009
    Posts:
    471
    Hey Little Angel,
    that is awesome absolute....
    well now i begin to understand how this work's
    thank you also for the example Code !!! :D
    Would be very nice to see this implemented in the Startrooper example for the next Unity3D iPhone release :)
    Also a Demo Project or complete Script should be on the WIKI so that there not so many Questions from newbies like me :oops:
    thank you very much...
    Little Angel :p

    kerstin :oops:
     
  28. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    You are correct about having this on the wiki.

    I'll try to get it up there.

    Also check in with the two sites from Will Goldstone:

    http://learnunity3d.com/
    http://learnmesilly.com/

    I'll put this up on learnunity3d as well as the wiki, but it may take me a few days as I've got both my game to finish and some (shudder) non-game work that I've got to keep on track.
     
  29. Muckel

    Muckel

    Joined:
    Mar 26, 2009
    Posts:
    471
    Code (csharp):
    1. var turnSpeed : float = 10.0;
    2. var maxTurnLean : float = 50.0;
    3. var maxTilt : float = 50.0;
    4. var sensitivity : float = 10.0;
    5. var forwardForce : float = 1.0;
    6. var guiSpeedElement : Transform;
    7. private var normalizedSpeed : float = 0.2;
    8. private var euler : Vector3 = Vector3.zero;
    9.  
    10.  
    11. var horizontalOrientation : boolean = true;
    12.  
    13. private var myTransform : Transform;
    14. private var myRigidbody : Rigidbody;
    15.  
    16.  
    17. function Awake () {
    18.     startPosition=this.transform.position;
    19.  
    20.     if (horizontalOrientation)
    21.         Screen.SetResolution(480, 320, true);
    22.     else
    23.         Screen.SetResolution(320, 480, true);
    24.        
    25.     guiSpeedElement.position = new Vector3 (0, normalizedSpeed, 0);
    26. }
    27.  
    28. function Start() {
    29.     // Cache "hidden" GetComponent calls for extra speed
    30.     myTransform = transform;
    31.     myRigidbody = rigidbody;
    32.  
    33.     GameController.instance.startPosition = transform.position;
    34.  
    35. }
    36.  
    37. function Update () {
    38.    
    39.     for (var evt : iPhoneTouch in iPhoneInput.touches)
    40.     {
    41.         if (evt.phase == iPhoneTouchPhase.Moved)
    42.         {
    43.             normalizedSpeed = evt.position.y / Screen.height;
    44.             guiSpeedElement.position = new Vector3 (0, normalizedSpeed, 0);
    45.         }
    46.     }
    47. }
    48.  
    49.  
    50. function FixedUpdate () {
    51.    
    52.     rigidbody.AddRelativeForce(0, 0, normalizedSpeed * forwardForce);
    53.    
    54.     var acceleration : Vector3 = iPhoneInput.acceleration;
    55.    
    56.     var fixedAcceleration : Vector3 = FixAcceleration (acceleration);
    57.    
    58.     if (horizontalOrientation)
    59.        {
    60.           var t : float = fixedAcceleration.x;
    61.           fixedAcceleration.x = -fixedAcceleration.y;
    62.           fixedAcceleration.y = t;
    63.        }
    64.    
    65.     // Rotate turn based on acceleration       
    66.     euler.y += fixedAcceleration.x * turnSpeed;
    67.     // Since we set absolute lean position, do some extra smoothing on it
    68.     euler.z = Mathf.Lerp(euler.z, -fixedAcceleration.x * maxTurnLean, 0.2);
    69.  
    70.     // Since we set absolute lean position, do some extra smoothing on it
    71.     euler.x = Mathf.Lerp(euler.x, fixedAcceleration.y * maxTilt, 0.2);
    72.    
    73.     // Apply rotation and apply some smoothing
    74.     var rot : Quaternion = Quaternion.Euler(euler);
    75.     transform.rotation = Quaternion.Lerp (transform.rotation, rot, sensitivity);
    76. }
    77.  
    78. //Used to calibrate the initial iPhoneIput.acceleration input
    79. function CalibrateAccelerometer () {
    80.    var accelerationSnapshot : Vector3 = iPhoneInput.acceleration;
    81.    var rotateQuaternion : Quaternion = Quaternion.FromToRotation(new Vector3(0.0, 0.0, -1.0), accelerationSnapshot);
    82.  
    83.    //create identity matrix ... rotate our matrix to match up with down vec
    84.    var matrix : Matrix4x4 = Matrix4x4.TRS(Vector3.zero, rotateQuaternion, new Vector3(1.0, 1.0, 1.0));
    85.  
    86.    //get the inverse of the matrix
    87.    calibrationMatrix = matrix.inverse;
    88.    GameController.instance.calibrationMatrix = matrix.inverse;  //Here you need a External Script who handles the Matrix - So i have a GameControler and there i use public var calibrationMatrix : Matrix4x4;
    89. }
    90.  
    91.  
    92.  
    93. //   Get the 'calibrated' value from the iPhone Input
    94. function FixAcceleration (accelerator : Vector3) {
    95.    var fixedAcceleration : Vector3 = calibrationMatrix.MultiplyVector(accelerator);
    96.    return fixedAcceleration;
    97. }
    So like Little Angel describes you need a external Script to store your Matrix like a GameControler... there i use:
    Code (csharp):
    1. public var calibrationMatrix : Matrix4x4;
    Now i have a Pause Screen where i have a Button that call the function CalibrateAccelerometer () in the Sript above... the rest is Magic ;-)

    Thank Little Angel for the great Share Explanation !!! it works :D

    kerstin :oops:
     
  30. Vern_Shurtz

    Vern_Shurtz

    Joined:
    Mar 6, 2009
    Posts:
    264
    Hi Muckel,

    Thank you for implementing the calibration into the startrooper script. I have created a new script and copied your code into it and get an error;

    Unknown identifier 'GameController'.

    Then i created a GameController.js script and copied your other code, " public var calibrationMatrix : Matrix4x4; " into it and saved it. I then get the error;

    'instance' is not a member of 'GameController'.

    I guess I'm missing something here. I've been trying to understand this calibration process and was hoping to get it working in the Startrooper project so I would have a working setup to examine.

    Thanks to everyone for sharing thier awesome knowledge....
    :)
     
  31. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Vern_S:

    Just a quick tho't...

    Sounds like a typing (not typo) issue.

    Have you checked whether you've strongly and specifically typed the variables that are trying to line "GameController"?
     
  32. Muckel

    Muckel

    Joined:
    Mar 26, 2009
    Posts:
    471
    Hi,
    i use this AManagerClass from the Wiki.
    I have remaned it to GameController.js
    so here is the Script with the one Line included...
    Code (csharp):
    1. /// AManager is a singleton.
    2.  /// To avoid having to manually link an instance to every class that needs it, it has a static variabe called
    3.  /// instance, so other objects that need to access it can just call:
    4.  ///        GameController.instance.DoSomeThing();
    5.  ///
    6.  static var instance : GameController;
    7.  
    8.  // This is where the magic happens.
    9.  //  FindObjectOfType(...) returns the first AManager object in the scene.
    10.  instance =  FindObjectOfType(AManager);
    11.  if (instance == null)
    12.      Debug.Log ("Could not locate an AManager object. \
    13.                     You have to have exactly one AManager in the scene.");
    14.  
    15.  // Ensure that the instance is destroyed when the game is stopped in the editor.
    16.  function OnApplicationQuit() {
    17.      instance = null;
    18.  }
    19.  
    20.  // Add the rest of the code here...
    21.  
    22. public var calibrationMatrix : Matrix4x4;
    23.  
    24.  function DoSomeThing() {
    25.      Debug.Log("Doing something now", this);
    26.  }
    27.  
    the Line:
    Code (csharp):
    1. public var calibrationMatrix : Matrix4x4;
    is call by this:
    Code (csharp):
    1. GameController.instance.calibrationMatrix = matrix.inverse;
    from the PlayerControls Script.

    kerstin :oops:
     
  33. Vern_Shurtz

    Vern_Shurtz

    Joined:
    Mar 6, 2009
    Posts:
    264
    Making some progress.

    I placed your AManager code into my GameController.js script and received an error relating to the forward slash "/" in the first Debug.Log line. I removed the forward slash "/" and that removed the error.

    I also have the error;

    Unknown identifier 'AManager' .

    I replaced FindObjectOfType(AManager) with FindObjectOfType(GameController) .

    That seems to get that script to compile without errors.

    Is this correct?

    Code (csharp):
    1.  
    2.  
    3. /// AManager is a singleton.
    4.  /// To avoid having to manually link an instance to every class that needs it, it has a static variabe called
    5.  /// instance, so other objects that need to access it can just call:
    6.  ///        GameController.instance.DoSomeThing();
    7.  ///
    8.  static var instance : GameController;
    9.  
    10.  // This is where the magic happens.
    11.  //  FindObjectOfType(...) returns the first AManager object in the scene.
    12.  instance =  FindObjectOfType(GameController);
    13.  
    14.  if (instance == null)
    15.      Debug.Log ("Could not locate an AManager object. You have to have exactly one AManager in the scene.");
    16.  
    17.  // Ensure that the instance is destroyed when the game is stopped in the editor.
    18.  function OnApplicationQuit() {
    19.      instance = null;
    20.  }
    21.  
    22.  // Add the rest of the code here...
    23.  
    24. public var calibrationMatrix : Matrix4x4;
    25.  
    26.  function DoSomeThing() {
    27.      Debug.Log("Doing something now", this);
    28.  }
    29.  
    30.  


    I have 2 errors in the PlayerControls script.

    'startPosition' is not a member of 'GameController'.

    and

    Unknown identifier: 'calibrationMatrix'.

    I've been trying to work through these errors but I'm hitting a mental barrier at this point.

    Any help would be most appreciated.
     
  34. Muckel

    Muckel

    Joined:
    Mar 26, 2009
    Posts:
    471
    Code (csharp):
    1. I have 2 errors in the PlayerControls script.
    2.  
    3. 'startPosition' is not a member of 'GameController'.
    4.  
    5. and
    6.  
    7. Unknown identifier: 'calibrationMatrix'.
    8.  
    9. I've been trying to work through these errors but I'm hitting a mental barrier at this point.
    10.  
    11. Any help would be most appreciated.

    Code (csharp):
    1. static var startPosition : Vector3;
    2. private var calibrationMatrix : Matrix4x4;.
    Put also in PlayerControls Script than it should work.
     
  35. Vern_Shurtz

    Vern_Shurtz

    Joined:
    Mar 6, 2009
    Posts:
    264
    I added those two line to the playercontrols script and get the following errors

    'startPosition' is not a member of 'GameController'.

    and

    'calibrationMatrix' is not a member of 'GameController'.


    Getting a working iPhone calibration capability added to the Star Trooper example would be extremely beneficial to many in the community and would help greatly to further understand the accelerometer and how it can be utilized in Unity.

    Please continue to help in this endeavor.

    :)
     
  36. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Vern:

    Many of these issues are personal setup compatibility issues. These are due to the specific way Mukel has set up her game. Look back through this thread and look at the base code and find a way to fit it into YOUR set up. You may not need the controller/manager script that she is using and is giving you these errors.

    You should be able to just take the base calibration code and add it into your set up without any of the additional scripts Mukel is using that are not part of the trooper demo.

    Personally I don't have a controller script like aManager, and the trooper demo probably doesn't need one either.

    The base code should just work simply by itself.
     
  37. Vern_Shurtz

    Vern_Shurtz

    Joined:
    Mar 6, 2009
    Posts:
    264
    I agree that everyone has their particular way of implementing calibration. I have already gone back to the code uploaded by Sandoze and have started working through from there.

    I still think that having a calibration setup inside Star Trooper Demo would be very beneficial to the community and will continue to work towards making that a reality.

    Kerstin has already implemented the code into Star Trooper Demo but so far, for me anyway, it is not working. All I was doing was trying to figure out why.

    I am a 3D Artist learning scripting for the first time. Things that may be clear as day to a programmer are still a bit confusing for me.
     
  38. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Vern:

    First off, let me say that I'm not a programmer, but I'm an artist and creative producer that hadn't written a line of code in his life before working with Unity.

    That being said, I've now been working regularly (tho' part time) with Unity for six months and have gone through the two basic Unity tutorials (not iPhone Demos) more than once, so this has given me a foundation to build from.

    I didn't mean to be offensive, but what I was trying to point out was that the problems you are encountering have nothing to do with implementing the Vans Sk8 code into the Star Trooper demo. The problems and errors you are encountering are from trying to use Muckel's code, which has been personalized to use other componenets that are not in the Star Trooper demo, and are confusing the issue.

    This is just thinking it through:

    You will need 4 pieces of code to make it work.

    A variable to save the calibration angle snapshot into.

    The calibration functions.

    A button that calls a calibration function.
    For simplicity make this an OnGUI button*.

    An add-on piece of code to wrap your iPhone input into to give you "fixed" input from the iPhone.

    then you need to adjust your code to use "fixedInput" rather than the original "rawInput".

    That's all you need to do.

    The AManager Class of Controller object is in the next grade of game management and should only be used once the basics are under control.

    -

    If you go back to page 2 where I show how I used the code, it will tell you how to use it and how to use these 4 bits.

    -

    To be more clear:

    Find the script in Star Trooper that takes the iPhone Input. This will probably be a script called Player Contoller or Ship Controller. It should have a line of code similar to:
    Code (csharp):
    1. var acceleration : Vector3 = iPhoneInput.acceleration;
    but it will definitely contain the code:
    Code (csharp):
    1. iPhoneInput.acceleration
    as that's where the input signals you need from the iPhone are coming from.

    In this script add the first necessary piece of code - the place to store the saved calibration setting:
    Code (csharp):
    1. private var calibrationMatrix : Matrix4x4;
    I always set up all my variables that the top of any script so I can see them, and know they've been created before any code.


    Then copy these functions into the script:
    Code (csharp):
    1. //Used to calibrate the initial iPhoneIput.acceleration input
    2. function CalibrateAccelerometer () {
    3.    var accelerationSnapshot : Vector3 = iPhoneInput.acceleration;
    4.    var rotateQuaternion : Quaternion = Quaternion.FromToRotation(new Vector3(0.0, 0.0, -1.0), accelerationSnapshot);
    5.  
    6.    //create identity matrix ... rotate our matrix to match up with down vec
    7.    var matrix : Matrix4x4 = Matrix4x4.TRS(Vector3.zero, rotateQuaternion, new Vector3(1.0, 1.0, 1.0));
    8.  
    9.    //get the inverse of the matrix
    10.    calibrationMatrix = matrix.inverse;
    11. }
    12.  
    13.  
    14.  
    15. //   Get the 'calibrated' value from the iPhone Input
    16. function FixAcceleration (accelerator : Vector3) {
    17.    var fixedAcceleration : Vector3 = calibrationMatrix.MultiplyVector(accelerator);
    18.    return fixedAcceleration;
    19. }
    I put this at the bottom of the script where I put all my custom functions. (Arguably this is TWO pieces of code, as it's one function to TAKE the snapshop, and one function to USE the snapshot...)


    Then create a button to activate your calibration:
    Code (csharp):
    1. if (GUI.Button(Rect((Screen.width / 2) - 75, Screen.height - 75, 150, 70), "Calibrate")) {
    2.     CalibrateAccelerometer ();
    3. }
    This button should put the button in the lower center of the screen. I've not tested the coordinates, but pulled them out of the air. Your guiSkin, if you have one, will effect this, as it will decide the typeface and button graphic.


    Then you need to wrap your input code so that it uses your calibration variable.

    Find the line of code that has:
    Code (csharp):
    1. iPhoneInput.acceleration;
    in it somewhere. It will be in function Update(), or function FixedUpdate(), as this is something that needs to be "updated" at least every frame.

    Depending on how they set it up it will be something like this:
    Code (csharp):
    1. //   Retrieve input from Accelerometer
    2.  var acceleration : Vector3 = iPhoneInput.acceleration;
    3.  
    4.  var accel : Vector3 = iPhoneInput.acceleration;
    5.  
    6.  var acceleration : Vector3
    7.  acceleration = iPhoneInput.acceleration;
    8.  
    9.  var accel : Vector3
    10.  accel = iPhoneInput.acceleration;
    The variable could be named anything from iPhoneInput to kingGeorgeIII, but most folks seem to use some form of the word acceleration. The variable could be set up before hand or done in one line. Both are valid. But you will find someVariable of type Vector3 that will equal iPhoneInput.acceleration.

    Once you find this critical line, you need to add on your calibration code:
    Code (csharp):
    1.    var fixedAcceleration : Vector3 = FixAcceleration (acceleration);
    so that the final code looks something like:

    Code (csharp):
    1. //   Retrieve input from Accelerometer
    2.    var acceleration : Vector3 = iPhoneInput.acceleration;
    3.    var fixedAcceleration : Vector3 = FixAcceleration (acceleration);
    To understand this code, look closely at what it's doing as you may have to change the variables slightly to match what they are using in the Demo.

    The Logic is:
    I'm using "var acceleration" as "Variable One".
    Variable One is set to the iPhone accelerator.
    Variable One is placed inside the () of the next line.
    Variable Two is set to equal the "fixed" value that comes back from (is returned from) function FixAcceleration() where Variable One is the Vector3 value in the brackets to be fixed.

    So - when this procedure is done, you now have "fixed acceleration" that should be "fixed" according to your calibrated snapshot.

    You now need to replace all instances of "Variable One", which I'm calling "var acceleration" with "fixedAcceleration", so the application when it's playing, uses the fixed value rather than the raw value from the phone.

    This means, if you have:
    Code (csharp):
    1. var thrust = acceleration.x"
    should be changed to:
    Code (csharp):
    1. var thrust = fixedAcceleration.x"
    otherwise the above code will be useless. You have to use your fixed input!

    Again, I've not looked at the Star Trooper demo, but this will work in any case where raw iPhone input is being used to drive a game and you want to use fixed input instead.



    * (Which choice of making functioning buttons on the iPhone is worth an entire technical conference...)
     
  39. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Not to confuse issues, but...

    If you get your head around the above post, there is one step that could simplify updating an existing script that would save you some retyping or refactoring of your script.

    Please don't let this post confuse what I'm saying above, BUT...

    To keep from rewriting the input value of the existing script, if your original line of code to take the iPhoneInput was:
    Code (csharp):
    1. var acceleration : Vector3 = iPhoneInput.acceleration;
    and you later use the code with lines like:
    Code (csharp):
    1. var thrust = acceleration.x;
    2. var turn = acceleration.y
    you could retain your use of the variable "acceleration" by changing the variable name in your original line of code and use "acceleration" as the variable in your "fixed" line.

    So:

    If you have:
    Code (csharp):
    1. //   Retrieve input from Accelerometer
    2.    var acceleration : Vector3 = iPhoneInput.acceleration;
    And above I suggested:
    Code (csharp):
    1. //   Retrieve input from Accelerometer
    2.    var acceleration : Vector3 = iPhoneInput.acceleration;
    3.    var fixedAcceleration : Vector3 = FixAcceleration (acceleration);
    which would require that you retype or refactor your script to use "fixed acceleration" as the variable containing the input driving your game,

    you could instead use the code:
    Code (csharp):
    1. //   Retrieve input from Accelerometer
    2.    var rawAcceleration : Vector3 = iPhoneInput.acceleration;
    3.    var acceleration : Vector3 = FixAcceleration (acceleration);
    which retains "acceleration" as your primary input variable.

    By changing your "Variable One" to "rawAcceleration" and setting your "Variable Two" to use the "original Variable One", this means the rest of your script will still be looking for "original Variable One" (acceleration) as it's input, and it should just work as it had before, except using the fixed value under the original variable name.

    (Don't forget to use whatever "Original Variable One" name was used in your script, not what I've used here!)
     
  40. Vern_Shurtz

    Vern_Shurtz

    Joined:
    Mar 6, 2009
    Posts:
    264
    Wow!!!

    Huge thanks for your time, patients (with my newbiness) and great explanation. Works great!!!! To me you are a "Big" Angel... :D

    Now in Krestin's code using AManager she was placing the Calibration Matrix in a separate file she called GameController that could be called on at anytime. This is the same thing you are doing with your Global File you call GameStatus?

    I take it that storing the Calibration Matrix in such a way would allow you to calibrate when you first run the game and then store that data for later use and you could add a "Calibrate" to the game settings menu to Re-Calibrate if needed.

    Is this a correct assumption? If so then I think I have enough hints to get this put into my setup..

    Thanks again for your help.

    Vern
     
  41. Vern_Shurtz

    Vern_Shurtz

    Joined:
    Mar 6, 2009
    Posts:
    264
    Here is the script for StarTrooper PlayerControls.js with Calibration using your instructions. Only issue I can find is that the guiSpeedElement no longer works once the calibration code and button are in.

    Edit: Loser error on guiSpeedElement not working. forwardForce default is at 1.0. Change in inspector to 50 and all works well. :?

    Code (csharp):
    1.  
    2.  
    3. var turnSpeed : float = 10.0;
    4. var maxTurnLean : float = 50.0;
    5. var maxTilt : float = 50.0;
    6. var sensitivity : float = 10.0;
    7. var forwardForce : float = 1.0;
    8.  
    9. var guiSpeedElement : Transform;
    10.  
    11. var horizontalOrientation : boolean = true;
    12.  
    13. private var normalizedSpeed : float = 0.2;
    14. private var euler : Vector3 = Vector3.zero;
    15.  
    16. // stores the saved calibration setting
    17. private var calibrationMatrix : Matrix4x4;
    18.  
    19. function Awake () {
    20.     if (horizontalOrientation)
    21.         Screen.SetResolution(480, 320, true);
    22.     else
    23.         Screen.SetResolution(320, 480, true);
    24.  
    25.     guiSpeedElement.position = new Vector3 (0, normalizedSpeed, 0);
    26. }
    27.  
    28. function Update () {
    29.     for (var evt : iPhoneTouch in iPhoneInput.touches)
    30.     {
    31.         if (evt.phase == iPhoneTouchPhase.Moved)
    32.         {
    33.             normalizedSpeed = evt.position.y / Screen.height;
    34.             guiSpeedElement.position = new Vector3 (0, normalizedSpeed, 0);
    35.         }
    36.     }
    37. }
    38.  
    39. function FixedUpdate ()
    40. {
    41.    
    42.    rigidbody.AddRelativeForce(0, 0, normalizedSpeed * forwardForce);
    43.    
    44.    var acceleration : Vector3 = iPhoneInput.acceleration;
    45.    
    46.    var fixedAcceleration : Vector3 = FixAcceleration (acceleration);
    47.    
    48.    if (horizontalOrientation)
    49.       {
    50.          var t : float = fixedAcceleration.x;
    51.          fixedAcceleration.x = -fixedAcceleration.y;
    52.          fixedAcceleration.y = t;
    53.       }
    54.    
    55.    // Rotate turn based on acceleration      
    56.    euler.y += fixedAcceleration.x * turnSpeed;
    57.    // Since we set absolute lean position, do some extra smoothing on it
    58.    euler.z = Mathf.Lerp(euler.z, -fixedAcceleration.x * maxTurnLean, 0.2);
    59.  
    60.    // Since we set absolute lean position, do some extra smoothing on it
    61.    euler.x = Mathf.Lerp(euler.x, fixedAcceleration.y * maxTilt, 0.2);
    62.    
    63.    // Apply rotation and apply some smoothing
    64.    var rot : Quaternion = Quaternion.Euler(euler);
    65.    transform.rotation = Quaternion.Lerp (transform.rotation, rot, sensitivity);
    66. }
    67.  
    68. //  button to activate your calibration
    69. function OnGUI () {
    70.     if (GUI.Button(Rect((Screen.width / 2) - 75, Screen.height - 75, 150, 70), "Calibrate")) {
    71.    CalibrateAccelerometer ();
    72.     }
    73. }
    74.  
    75. //Used to calibrate the initial iPhoneIput.acceleration input
    76. function CalibrateAccelerometer () {
    77.    var accelerationSnapshot : Vector3 = iPhoneInput.acceleration;
    78.    var rotateQuaternion : Quaternion = Quaternion.FromToRotation(new Vector3(0.0, 0.0, -1.0), accelerationSnapshot);
    79.  
    80.    //create identity matrix ... rotate our matrix to match up with down vec
    81.    var matrix : Matrix4x4 = Matrix4x4.TRS(Vector3.zero, rotateQuaternion, new Vector3(1.0, 1.0, 1.0));
    82.  
    83.    //get the inverse of the matrix
    84.    calibrationMatrix = matrix.inverse;
    85. }
    86.  
    87. //   Get the 'calibrated' value from the iPhone Input
    88. function FixAcceleration (accelerator : Vector3) {
    89.    var fixedAcceleration : Vector3 = calibrationMatrix.MultiplyVector(accelerator);
    90.    return fixedAcceleration;
    91. }
    92.  
    93.  
    94.  
     
  42. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Vern,

    You are correct that storing the data in some form of global file would help if you wanted the calibration to persist beyond the current level.

    There are many ways this can be done, including PlayerPrefs (http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html), a static variable, a variable you "don't destroy on load" or a game controller.

    That is beyond the scope of this thread, but if you look in the forums for a relevant thread, you'll find a lot of information of these subjects.
     
  43. Vern_Shurtz

    Vern_Shurtz

    Joined:
    Mar 6, 2009
    Posts:
    264
    Thanks again Little Angel for sharing your knowledge.

    I have already split out the calibrate button to a separate script and are using a static variable to transfer the info. I believe I am well on my way to getting the whole calibration process implemented and saved.

    Properly calibrating the iPhone has made quite a difference in the player functionality in my game. Now I can continue work on my game levels.

    Cheers...
     
  44. dawvee

    dawvee

    Joined:
    Nov 12, 2008
    Posts:
    276
    I had just implemented something similar, because man did it bug me that I had to hold the device flat to control the StarTrooper example!

    But looking over the code you posted, LittleAngel, could you elaborate on why you used an inverse TRS matrix to do this? I had decent results with the following code:

    Code (csharp):
    1. var restPosition = Vector3(0,0,-1);
    2. var calibrationRotation : Quaternion;
    3.  
    4. function Calibrate()
    5. {
    6.     var acceleratorSnapshot = iPhoneInput.acceleration;
    7.     calibrationRotation = Quaternion.FromToRotation(acceleratorSnapshot, restPosition);
    8. }
    9.  
    10. //And then in whatever function your game control code is:
    11. var accelerator = iPhoneInput.acceleration;
    12. accelerator = calibrationRotation * accelerator;
    It's very similar to your example, but it just seems simpler to me to apply only a rotation, since there's no scaling or translation involved.
     
  45. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Oh dear!

    No! I can't!

    If you look back on this thread, you'll see I'm just a humble user who translated this code - originally from FuelGames for their Vans Sk8 title - from C# to Unity.js.

    I've then been letting folks know how I've implemented it.

    Those matrix 4x4s are a bit of a black box for me. I've seen them used in the Lerpz demo, but have only used them when I've kit-bashed someone elses code.

    You could try PMing the folks a fuel?
     
  46. dawvee

    dawvee

    Joined:
    Nov 12, 2008
    Posts:
    276
    No worries, Little Angel! :) I was just wondering if there were an optimisation aspect or something I was missing out on doing it my way.

    I'll send the Fuel Games folks a pm and see if they might know.

    The Matrix4x4 is usually just a way of combining translation, rotation and scaling into one variable you can apply to a vector (there's also projection matrices, but those aren't used much in gameplay scripting code).

    In the Fuel example they:

    1. set the FromToRotation to rotate the rest position to the snapshotted accelerometer position,

    2. create a matrix that contains that rotation, zero translation and no scaling, and

    3. take the inverse of that matrix and apply it to the input (so it rotates the input in reverse of the rotation they calculated at step 1).

    But I figured you could cut out step 2 and 3 by just setting the FromToRotation the other way around, and then just applying the rotation by itself.

    The only potential drawback would be if matrix operations are faster than Vector3 * Quaternion, but you can still skip inverting the matrix. Anyway, I'll see if the Fuel folks can clarify. Watch this space! :p
     
  47. Muckel

    Muckel

    Joined:
    Mar 26, 2009
    Posts:
    471
    Hey Dawvee,
    also cool example....
    the question is :
    witch one has a better performance ???
    or is it the Same ?
    what do you think ???

    kerstin :oops:
     
  48. dawvee

    dawvee

    Joined:
    Nov 12, 2008
    Posts:
    276
    That's what I was wondering myself. I pm'ed someone at Fuel Games to ask them but haven't heard back yet. So far I've been using my method and I haven't noticed any performance hit. Plus the code is simpler to understand. So unless there's a big difference in performance (which doesn't seem to be the case), I'll stick with the simpler method, personally.
     
  49. Crazy Robot

    Crazy Robot

    Joined:
    Apr 18, 2009
    Posts:
    921
    Hello,

    First off, I’m such a newbie to this it’s not funny (3 weeks), so please go easy on me.

    Over the last week or so I have searched for a way to deactivate the player controls when the player dies then respawns, then reactive the controls.

    I have it setup when the player dies, the player object hides so the explosion can happen, then respawns and unhides. The problem is when the object is hidden the accelerometer inputs are still active, so if the player moves the iPhone, during this process, the object respawns off center.

    So, is there a way to temporarily disable the controls so when he dies, the controls are frozen and after respawn, the controls are activated again?

    Thanks,
    JL
     
  50. huxley

    huxley

    Joined:
    Apr 27, 2009
    Posts:
    334
    I've successfully gotten my calibrate function working in my player script, but I'm having problems getting the calibration settings to persist.

    When I complete a level and then load the player script again in a new level the calibrationMatix is not loaded. Thus, I must press the calibrate button again to set the calibrationMatrix.

    Here is my GameControl.js:
    Code (csharp):
    1.  
    2. public var calibrationMatrix : Matrix4x4;
    3.  
    4. // ----------- basic game functions ---------------
    5.  
    6.  
    7. function Start (){
    8.     // Make sure that the gamecontroller always survives level loads
    9.     DontDestroyOnLoad (this);
    10.      // This is where the magic happens.
    11.  //  FindObjectOfType(...) returns the first AManager object in the scene.
    12.  instance =  FindObjectOfType(GameController);
    13.  if (instance == null)
    14.      Debug.Log ("Could not locate an AManager object.You have to have exactly one AManager in the scene.");
    And then I have called the calibrateMatrix var from GameController in my player control script FixedUpdate function, as suggested in this thread.
    Code (csharp):
    1.  
    2. private var calibrationMatrix : Matrix4x4;
    3.  
    4.     //This is the advanced accellerometer code to calibrate the initial iPhoneIput.acceleration input
    5. function CalibrateAccelerometer () {
    6.    var accelerationSnapshot : Vector3 = iPhoneInput.acceleration;
    7.    var rotateQuaternion : Quaternion = Quaternion.FromToRotation(new Vector3(0.0, 0.0, -1.0), accelerationSnapshot);
    8.    //create identity matrix ... rotate our matrix to match up with down vec
    9.    var matrix : Matrix4x4 = Matrix4x4.TRS(Vector3.zero, rotateQuaternion, new Vector3(1.0, 1.0, 1.0));
    10.    //get the inverse of the matrix
    11.    calibrationMatrix = matrix.inverse;
    12.    //Here you need a External Script who handles the Matrix - So i have a GameControler and there i use public var calibrationMatrix : Matrix4x4;
    13.     GameController.instance.calibrationMatrix = matrix.inverse;
    14.     Debug.Log ("we've passed the calibrationMatrix for the GameController Script.");
    15.        }
    16.    // Make a button to set Calibration function
    17.    function OnGUI () {
    18.     GUI.Box (Rect (160,230,170,60), "Tilt to desired view angle");
    19.     if (GUI.Button (Rect (170,250,150,30), "Calibrate Accelerometer")) {
    20.     CalibrateAccelerometer ();
    21.     }
    22.     }
    23.  
    24.     //   Get the 'calibrated' value from the iPhone Input
    25. function FixAcceleration (accelerator : Vector3) {
    26.    var fixedAcceleration : Vector3 = calibrationMatrix.MultiplyVector(accelerator);
    27.    return fixedAcceleration;
    28. }
    29.  
    30. function FixedUpdate () {
    31.     //This in the new user calibrated acceleration input code it works, but we need to transfer the calibration to the players lookat box
    32.     var acceleration : Vector3 = iPhoneInput.acceleration;
    33.     // Retrieve input from Calibrated Accelerometer
    34.     var fixedAcceleration : Vector3 = FixAcceleration (acceleration);
    35.     var force = fixedAcceleration.x * speed;
    36.     var torque = -fixedAcceleration.y * speed;
    37.     rigidbody.AddRelativeForce (torque, 0, force);
    38. }
    39.  
    Any ideas why my global calibrationMatrix isn't being loaded when I reload my PlayerScript?