Accelerometer Code Nugget...
by Dan Pereira · in iTorque 2D · 04/08/2009 (3:36 pm) · 0 replies
I needed to be able to know how the user is holding the device. Drop this hack into your accelerometer behavior (i.e. shooterControls.cs in behavior shooter) and you will be able to tell how the user is holding the device and when they rotate it. This doesn't detect orientation, this detects movement.
function joystickMoveX(%val)
{
$directionX=%val;
}
function joystickMoveY(%val)
{
$directionY=%val;
}
function joystickMoveZ(%val){
if($directionY > $directionX){
if($directionY > 0.5){
$direction="Landscape";
} else {
$direction="Upside Down Portrait";
}
if($olddir!=$direction){
//Do something on rotate...
}
$olddir=$direction;
} else {
if($directionY > -0.5){
$direction="Portrait";
} else {
$direction="Upside Down Landscape";
}
if($olddir != $direction){
//Do something on rotate...
}
$olddir=$direction;
}
}