## 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

## Solutions/Answers:

### Answer 1:

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);
rigidbody2D.AddTorque(torque);
}
```

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;
rigidbody.AddTorque(torque);
}
```

### Answer 2:

take a look at Elastomania. I guess this is the game you want to replicate. there are some people with unbelievable skill at this game, check youtube 🙂

Anyway you need to take a look at inertia tensor tool. its a matrix that characterizes the solid’s inertia parameters.

http://en.wikipedia.org/wiki/Moment_of_inertia

In your case there are many terms that simplify because you are in 2D. Also you are not required to realistically calculate the second area moment http://en.wikipedia.org/wiki/Second_moment_of_area, this is done from geometry by integrating units of surface multiplied by their mass density. You can just estimate some value and create a small physics integrator that will do the simulation in time.

Your problem is 1D for the rotation because you have only one axis to care about. But you need also to solve contacts at the same time. It means you will need to apply forces at contact points, those forces are evaluated by the collision intersector, it will give you a list of impulses that correspond to the force you need to apply to avoid penetration.

If you add all forces you get a “net force” http://en.wikipedia.org/wiki/Net_force and for the torque you need to go through a little cross product and sum the result http://en.wikipedia.org/wiki/Torque. This will give you the “net torque” at some point you define as being the rotation center. I suppose it could be any point in fact. Just don’t forget to add the gravity force from the gravity center (usually geometrical center) of your bike and also sum in into the net torque.

## References

- Database Administration Tutorials
- Programming Tutorials & IT News
- Linux & DevOps World
- Entertainment & General News
- Games & eSport