I am designing an endless runner style game like temple run. I need to move the player left and right based on the accelerometer input (tilting). The problem is that the accelerometer input is very sensitive. In other words the player is moving left even if the phone is held straight. I want to make the player to move smoothly like temple run game. (Player has a Rigidbody
component attached to him.). Here is my initial code:
if (Input.acceleration.x < 0)
{
this.transform.Translate(-0.3f, 0, 0);
}
else if (Input.acceleration.x > 0)
{
this.transform.Translate(-0.3f, 0, 0);
}
Then I tried:
// Player is running in negative direction
// Input.acceleration > 0 moves the player right
if (Input.acceleration.x < -0.2)
{
// this.transform.Translate(-0.3f, 0, 0);
Vector3 dir = Vector3.zero;
dir.x = Input.acceleration.x;
if(dir.sqrMagnitude > 1)
{
dir.Normalize();
}
dir = dir * Time.deltaTime * horizontalMoveSpeed;
transform.Translate(dir.x, 0 ,0 );
}
else if (Input.acceleration.x > 0.2)
{
// this.transform.Translate(0.3f, 0, 0);
Vector3 dir = Vector3.zero;
dir.x = Input.acceleration.x;
if (dir.sqrMagnitude > 1)
{
dir.Normalize();
}
dir = dir * Time.deltaTime * horizontalMoveSpeed;
transform.Translate(-dir.x, 0, 0);
}
The problem in the edited new code is that the player moves only to the left even if the phone is tilted right. What is the mistake in my code?
I know that this question will get lots of down votes because this is a basic question. But unfortunately I am new to Unity and I cannot figure it out. What is the mistake?