I've been having this specific problem bugging me for quite some time now.
Here is a little overview of what I want my android app to do...
The phone held is parallel to the ground, let's say that my right and left are on X-Axis, front and back are on Z-Axis.
I have a cube, it's floating. When I tilt/shake/move my phone to the right (X), I want the cube to move(star falling) to that direction but with the force I used to move my phone so that the cube can fall just like I threw from my own hand. Same goes for left (-X), forward-left (Z,-X) and forward-right(Z,X).
Here is some code I found and modified but I faked the force and that's not what I want to do.
private var AccelerometerUpdateInterval : float = 1.0 / 60.0;
var LowPassKernelWidthInSeconds : float = 1.0; // The greater the value of LowPassKernelWidthInSeconds, the slower the filtered value will converge towards current input sample (and vice versa). You should be able to use LowPassFilter() function instead of avgSamples().
private var LowPassFilterFactor : float = AccelerometerUpdateInterval / LowPassKernelWidthInSeconds;
private var lowPassValue : Vector3 = Vector3.zero; // should be initialized with 1st sample
private var acceleration : Vector3;
private var deltaAcceleration : Vector3;
var force = 500.0;
function LowPassFilter(newSample : Vector3) {
lowPassValue = Vector3.Lerp(lowPassValue, newSample, LowPassFilterFactor);
return lowPassValue;
}
function FixedUpdate () {
acceleration = Input.acceleration;
deltaAcceleration = acceleration-LowPassFilter(acceleration);
if((deltaAcceleration.x) >=.3)
{
rigidbody.AddForce(Vector3.right * force * Time.deltaTime);
rigidbody.useGravity = true;
}
if((deltaAcceleration.x)>=.5)
{
rigidbody.AddForce(Vector3.right * (2 * force) * Time.deltaTime);
rigidbody.useGravity = true;
}
if((deltaAcceleration.x)>=.7)
{
rigidbody.AddForce(Vector3.right * (3 * force) * Time.deltaTime);
rigidbody.useGravity = true;
}
if((deltaAcceleration.x)>=.9)
{
rigidbody.AddForce(Vector3.right * (4 * force) * Time.deltaTime);
rigidbody.useGravity = true;
}
if((deltaAcceleration.x)<=-0.3)
{
rigidbody.AddForce(Vector3.left * force * Time.deltaTime);
rigidbody.useGravity = true;
}
if((deltaAcceleration.x)<=-0.5)
{
rigidbody.AddForce(Vector3.left * (2 * force) * Time.deltaTime);
rigidbody.useGravity = true;
}
if((deltaAcceleration.x)<=-0.7)
{
rigidbody.AddForce(Vector3.left * (3 * force) * Time.deltaTime);
rigidbody.useGravity = true;
}
if((deltaAcceleration.x)<=-0.9)
{
rigidbody.AddForce(Vector3.left * (4 * force) * Time.deltaTime);
rigidbody.useGravity = true;
}
if((deltaAcceleration.z) >=.3)
{
rigidbody.AddForce(Vector3.forward * (2 *force) * Time.deltaTime);
rigidbody.useGravity = true;
}
if((deltaAcceleration.z)>=.5)
{
rigidbody.AddForce(Vector3.forward * (4 * force) * Time.deltaTime);
rigidbody.useGravity = true;
}
if((deltaAcceleration.z)>=.7)
{
rigidbody.AddForce(Vector3.forward * (6 * force) * Time.deltaTime);
rigidbody.useGravity = true;
}
if((deltaAcceleration.z)>=.9)
{
rigidbody.AddForce(Vector3.forward * (8 * force) * Time.deltaTime);
rigidbody.useGravity = true;
}
}
Please help, and thanks in advance!
↧