Bug in rotation logic

Because of the if/elseif/else logic for rotation, if you start by pressing A, keep it pressed while pressing D, then release A, you will get into the right rotation block without passing through the else case, so you will end up with both side boosters running until you release both A and D.

The correct approach should instead be:

private void ProcessRotation() {
  if (Input.GetKey(KeyCode.A)) {
    RotateLeft();
  }
  else {
    StopRotatingLeft();

    if (Input.GetKey(KeyCode.D)) {
      RotateRight();
    }
    else {
      StopRotatingRight();
    }
  }
}

This still has the issue that it gives priority to a key over another (A over D), so it would probably be best to keep a state of the key that is already pressed, and continue rotating in that direction until it’s released, before taking into account the other key.

2 Likes

I know it’s old topic, but somebody might see this issue too (just like me)! :slight_smile:

You are right here, but this code might not be easy to read later. Usually we don’t want to enter too many if’s.
By the way you have fixed left rotation issue, but there is still right one to fix.
If you do your scenario but with D → A → D, you won’t achieve your “else else” case.

We should cross block animation. While we start left one, we want to make sure right is off. Other way we want the same.

Example here:

void ProcessRotation()
    {
        if (Input.GetKey(KeyCode.A))
        {
            if (!rightThrusterParticles.isPlaying)
            {
                leftThrusterParticles.Stop();
                rightThrusterParticles.Play();
            }
            ApplyRotation(rotationThrust);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            if(!leftThrusterParticles.isPlaying)
            {
                rightThrusterParticles.Stop();
                leftThrusterParticles.Play();
            }
            ApplyRotation(-rotationThrust);
        } 
        else
        {
            rightThrusterParticles.Stop();
            leftThrusterParticles.Stop();
        }
    }
2 Likes

Privacy & Terms