0

I am making a somewhat-racing game. The car automatically moves forward, but to turn it sideways, I measure the rotation of the phone. Since I have to measure the acceleration on the x axis, I use:

Direction.x = Input.acceleration.x * Time.deltaTime; 
Transform.translate (Direction.x * 5f);

When I play the game, the car rotates how I want it to when I tilt the phone on the x-axis. However, the problem is when I place the phone on the table, the car travels left super slowly, which doesn't make sense since it is at a 0 degree angle. To make sure this wasn't because of the table surface, I played it in Unity Simultator and same thing happened. The car travels left super slowly. When I debug.log, it says that Direction.x is about -0.000147..., a super small number. Is there any way to fix this problem, so that when the phone is still, the car's Direction.X will be 0, or is something wrong with my code.

1 Answers1

0

Sometimes in Unity Translate function glitches, because better use of standard operations with vectors. Just try to cut the minimum values of the accelerometer:

float min_value = 0.01f

if(Mathf.Abs(Input.acceleration.x) < min_value)
    Direction.x = Input.acceleration.x * Time.deltaTime;
else
    Direction.x = 0;

transform.position = transform.position + Direction.x * 5f;
OnionFan
  • 479
  • 3
  • 10