# Accelerometer bike game rotation help

## Accelerometer bike game rotation help

```Hi I've been making a game for the last few months and not sure what to do.
It's a 2d bike game and I want the rotation done with accelerometer, at the moment To test on computer I've set the rotation up as if left is hit transform.rotate to the left but because gravity my bike will not stay doing a wheelie.
Is there another way to do this can I just set up that the rotation of the bike is equal to the screen rotation? Or do I need the bike to stay in the air first and then when rotated right it goes right ect?
Really not sure and have been stuck on this for a month noob to unity too thanks
```

The way I’ve handled issues like this in the past is to keep track of a “desired angle/orientation”, and then each FixedUpdate I apply a torque to nudge the object toward that orientation.

You might implement this with a negative feedback control system, or error-controlled regulation, where as the bike deviates further from the goal, you apply greater and greater corrections to push it back. Here’s one way this might look:

(in 2D)

``````// This controls the maximum torque ever applied.
// without this, a bike far from the desired angle can be twisted too violently.
public float maxTorque;

// This controls how quickly the correction ramps-up as the bike drifts from the target.
// Higher values will keep it locked tighter, but it also might appear to jerk or vibrate.
public float rotationStiffness;

// This member variable will track the desired orientation of the bike.
// Let's keep it in the range 0 to 360 degrees, to match rigidbody2D.rotation
float desiredRotation;

void FixedUpdate()
{
// Find the angular correction needed to get back to the desired rotation.
float correction = desiredRotation - rigidbody2D.rotation;

// Wrap the angular correction
// (We'd rather turn 170 degrees counter-clockwise than 190 degrees clockwise)
if(Mathf.abs(error) > 180f)
correction -= Mathf.sign(correction) * 360f;

// Calculate a scaled torque based on the correction & public parameters.
float torque = Mathf.clamp(correction * rotationStiffness, -maxTorque, maxTorque);

}
``````

This is a bit different if you’re using 3D physics, since a rotation is more than a single number. You could extract the rotation angle and do the same thing, but here’s a similar method using vectors:

``````// Keep maxTorque & rotationStiffness as before.

// This member variable will track the desired orientation of the bike.
// This time we're storing it as a direction we want the bike's "forward" to point.
Vector3 desiredForward;

void FixedUpdate()
{
// You might need to flip the order of transform.forward & desiredForward here.
// It's easy to get mixed up with left-vs-right-handed coordinates, so this is a guess.
Vector3 torque = rotationStiffness * Vector3.Cross(transform.forward, desiredForward);

// If your body's rotation is not constrained on the other two axes,
// you may want to project this torque onto the pivot axis to avoid wobble.

// Ensure our maximum torque is respected.
if (torque.sqrMagnitude > maxTorque * maxTorque)
torque *= maxTorque/torque.magnitude;

}
``````