#Flying a spaceship through space and need help keeping it oriented...

1 messages · Page 1 of 1 (latest)

ripe parcel
#

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

  1. When a high speed collision happens the ship goes into a flat spin and never recovers. Insert Top Gun joke here.
  2. If it's a gentle spin it will be knocked off vertical and then take forever to recover.
  3. 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?
polar locust
#

You mustn't use Lerp to interpolate angles. Use LerpAngle instead when working with angles.

Aside from that, this is not how Lerp is supposed to be used. See this message <#old_programming message> for an example how to properly lerp.
Or, use SmoothDampAngle instead of Lerp/LerpAngle in your code.

ripe parcel
#

Ok, so thank you! Give me a few to get back into code.

#

@polar locust if I issue a coroutine, can I interrupt it? I'm thinking what if that coroutine is working the ship back to vert and then it get's hit again before completing the lerp? I should interrupt the first and start a new??

polar locust
#

StartCoroutine returns a Coroutine instance. If you cache that in a variable, you can call StopCoroutine on it to interrupt the execution of that coroutine.

ripe parcel
#

So something like (pseudo code)

'if !stabilize
stabilize.StopCoroutine;
stabilize = StartCoroutine(KeepShipVertical(duration))'