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;

}

• Hi thank you for answering me. The overall outcome I want is the bikes rotation is equal to the screen held in a certain place and when tilted the bike wheelies more or less, is this possible with this code? Or will when tilted the bike just go back to the same rotation point? Thanks – Sam Feb 25 '15 at 18:41
• The bike will seek to match its rotation to the "desiredRotation" variable - it's up to you to set that rotation value in whatever way is appropriate for your game. – DMGregory Feb 25 '15 at 20:35
• Hi thanks a lot this is what I've been looking for but I'm have a few questions, it's in 3d even tho one axis isn't use does this change the code? And if I wanted it to move nothing or very little before moving back which part do I change? Thank you so much – Sam Feb 28 '15 at 16:17
• Sorry for the delay. If you're using 3D physics, you should probably emphasize that in the question (which currently refers only to 2D), as the math & methods available can be quite different. I'll update the answer with a 3D version... – DMGregory Mar 16 '15 at 22:39

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.

• The asker tagged the question "unity", so the built-in physics engine will handle most of the things v.oddou describes here. Instead, the asker seems to be asking about how to maintain a particular orientation/force/torque, rather than applying it only instantaneously on certain events. – DMGregory Feb 24 '15 at 3:25
• Oh right. maybe I could delete this answer if something good comes up on the side later then. – v.oddou Feb 24 '15 at 3:52
• Hi thanks for the answers maybe my question wasn't clear I just want the bike to stay rotated (wheelie) until tilted and adjust for terrain but freezing rotation and angular drag don't help thanks – Sam Feb 24 '15 at 18:23