- Home /
Question by
spiceboy9994 · Jan 09, 2014 at 07:36 PM ·
iosaccelerometeraccelerationinput.accelerationphysics.gravity
Input.acceleration throws "Index out of bounds"
Hi:
I have a game that alters Physics gravity by using the device accelerometer and other variables. So, I have something like this.
void FixedUpdate()
{
//...
DoUpdateGravity();
}
void DoUpdateGravity()
{
Physics.Gravity = GetGravity();
//Do some stuff and apply gravity forces
}
Vector3 GetGravity()
{
Vector3 accel;
#if UseDeviceOrientationForGravity
try
{
//When my device is not moving, the following line throws and index out of range Exeption
accel = Input.acceleration.normalized;
/*this is the xcode detailed Error:
UnityException: Index out of bounds.
at UnityEngine.Input.get_accelerationEvents () [0x00000] in <filename unknown>:0*/
}
catch (Exception ex)
{
accel = Vector3.Zero.normalized;
}
accel*= m_GravityForce;
accel.x = -accel.x;
#if TweakGravityVector
float s = Mathf.Sin( m_TweakGravityVectorAngle );
float c = Mathf.Cos( m_TweakGravityVectorAngle );
Vector3 tmp1 = accel * c;
Vector3 tmp2 = Vector3.right * ( 1 - c ) * Vector3.Dot( Vector3.right, accel );
accel = Vector3.Cross( accel, Vector3.right );
accel*= s;
accel+= tmp1;
accel+= tmp2;
#endif
return accel;
}
I'm using unity 4.3.2f1 and xcode 5.0 (this is an iOS Unity3d unity game). I don't know why I'm getting this error, but following code makes my character to shake as crazy, and the shaking increases over time. The weird thing is that on debug mode, the application does not break due the exception, the execution continues, just the shaking issue. After I placed the try catch, shaking stopped and exception was catched and handled, but my character does not respond to device tilting and shaking anymore. Any guidance on this issue?
Thanks a lot
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613125957im_/https://answers.unity.com/themes/thub/images/avi.jpg)