Designing a SHMUP with the spaceship constantly "flying" towards the top of the screen. That's simply the backdrop moving downward and your ship has movement directly controlled by gamepad/keyboard with no inertia or thrust of it's own.
The game design has asteroids constantly 'falling' from top towards bottom of the screen. Eventually there will be enemies as well. On the occasion that a rock hits the ship I want the ship to be thrown off on the Z axis between -45 and 45 but then come back to 0 over a second or two. Here's the code that I am currently running...
` public float correctRotation = 0.0f;
public float smoothness = 50;
void KeepShipVertical()
{
Vector3 currentRotation = transform.eulerAngles;
currentRotation.z = Mathf.Clamp(currentRotation.z, -45.0f, 45.0f);
currentRotation.z = Mathf.Lerp(currentRotation.z, correctRotation, Time.deltaTime * smoothness);
transform.eulerAngles = currentRotation;
}`
Both the ship and asteroids are dynamic ridgidbody 2D's, with zero gravity. The ship has a mass of 1 and the rocks have mass of 10, in case that matters. KeepShipVertical is being called each Update.
Three challenges...
- When a high speed collision happens the ship goes into a flat spin and never recovers. Insert Top Gun joke here.
- If it's a gentle spin it will be knocked off vertical and then take forever to recover.
- Once back to 0 the ship will 'wobble' back and forth, isn't there some micro small math number I can use to dampen out that wobble?