- Home /
Question by
InfinityDrive · Jan 08, 2013 at 01:51 AM ·
accelerometertransform.rotateflight
jittery movement when using transform.up and transform.Rotate
Hello, I been working on a flight script for a while and came across some code, however its not working properly when I try to add world rotation around Y-axis when the local euler angle z increases. So, when the airplane is at a 0 degree roll nothing happens, at >0 or <360 it begins to turn left around world y axis multiplied by angle of roll. Here is the code, the airplane starts to twitch a lot.. I am assuming transform.up does not update with the new position of the airplane and reverts to original?
Additionally, I am not sure how to adjust the accelerometer to the users neutral holding andgle..
#pragma strict
var minVelocity : float = 10;
private var sizeFilter: int = 10;
private var filter: Vector3[];
private var filterSum = Vector3.zero;
private var posFilter: int = 0;
private var qSamples: int = 0;
var eulerx : float;
var eulery : float;
var eulerz : float;
function Start ()
{
}
function Update ()
{
transform.Translate( 0, 0, minVelocity * Time.deltaTime ); //velocity
eulerz = transform.localEulerAngles.z;
var temp: Vector3 = MovAverage( Input.acceleration.normalized );
transform.up.z = temp.normalized.x;
transform.up.x = -temp.normalized.y;
//issue here
//left turn
if( eulerz > 0 && eulerz < 90 )
{
transform.Rotate( 0, - eulerz * Time.deltaTime * 10, 0, Space.World );
}
//right turn
if( eulerz > 270 && eulerz <360 )
{
transform.Rotate( 0, eulerz * Time.deltaTime * 10, 0, Space.World );
}
}
function MovAverage( sample: Vector3 ) : Vector3
{
if( qSamples == 0 ) filter = new Vector3[ sizeFilter ];
filterSum += sample - filter[ posFilter ];
filter[ posFilter++ ] = sample;
if ( posFilter > qSamples ) qSamples = posFilter;
posFilter = posFilter % sizeFilter;
return filterSum / qSamples;
}
Comment