I need to convert this “shake” function from obj-c to unity… any help would be appreciated.
Thanks
- (void) accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
UIAccelerationValue length,
x,
y,
z;
//Use a basic high-pass filter to remove the influence of the gravity
myAccelerometer[0] = acceleration.x * kFilteringFactor + myAccelerometer[0] * (1.0 - kFilteringFactor);
myAccelerometer[1] = acceleration.y * kFilteringFactor + myAccelerometer[1] * (1.0 - kFilteringFactor);
myAccelerometer[2] = acceleration.z * kFilteringFactor + myAccelerometer[2] * (1.0 - kFilteringFactor);
// Compute values for the three axes of the acceleromater
x = acceleration.x - myAccelerometer[0];
y = acceleration.y - myAccelerometer[0];
z = acceleration.z - myAccelerometer[0];
//Compute the intensity of the current acceleration
length = sqrt(x * x + y * y + z * z);
// If above a given threshold, play the erase sounds and erase the drawing view
if((length >= kEraseAccelerationThreshold) (CFAbsoluteTimeGetCurrent() > lastTime + kMinEraseInterval)) {
//DO STUFF
}
}
The whole accelerator part can be dropped for iPhoneInput.acceleration, a Vector3 object that contains the current acceleration value.
Addition things are present there, you only need to search for iPhone in the Unity iPhone Scripting Reference to see all the possible iphone related settings and input capabilities, with iPhoneInput covering the input part.
The rest below that does the calculation can more or less be copy pasted
how do i raise something to a power
var length = (x * x + y * y + z * z) ^ (1 / 2);
does not seem to work…
Mathf.Pow(). (Love using that one…I just wish we had Mathf.Zap() and Mathf.Blam().)
–Eric
Thanks Guys!
haha Eric! nice one!
if anyone cares… here is the code
var accel : Vector3 = iPhoneInput.acceleration;
var kEraseAccelerationThreshold = 2.0;
var x = accel.x;
var y = accel.y;
var z = accel.z;
var length = Mathf.Pow((x * x + y * y + z * z), (1 / 2));
if (length >= kEraseAccelerationThreshold) {
// DO STUFF
}
it compiles… am i missing anything?
var kEraseAccelerationThreshold = 2.0;
function Update () {
var accel = iPhoneInput.acceleration;
var x = accel.x;
var y = accel.y;
var z = accel.z;
var length = Mathf.Pow((x * x + y * y + z * z), (1 / 2));
if (length >= kEraseAccelerationThreshold) {
// Destroy (GameObject.Find("Splatter(Clone)"));
Application.LoadLevel (0);
}
}
That is the full code… it doesnt work… it compiles fine… but nothing happens
any help would be appreciated
thanks
Always use debug statements. That way you can find out what the code is actually doing. (Although I can tell that 1/2 is going to give you 0, since that’s integer division.)
–Eric
How should i square root (x * x + y * y + z * z) then?
ahh found it, Mathf.Sqrt.
trying that
It works!
thanks!
for my next noobish question… is there a way to destroy all objects with a name of “Splatter(clone)” at the same time?
You could always have used floating point division. 1.0/2.0 rather than 1/2. Better yet, just use .5, since division is usually expensive.
–Eric