1
votes

I have a very simple model for a plane that can move around in the x and y directions, while an ocean scrolls by to make it look as though the plane is flying. Whenever the plane moves, I adjust its roll and pitch.

I compute and maintain the position, roll and pitch, inside the object script. Every update step I re-assign them to the Unity properties of the object.

The problem comes where I tried to add a barrel-roll effect. This code works fine:

    transform.Translate(position - transform.position);

    Vector3 rotChange = new Vector3 (-pitch, 0, -roll) -                 
                    transform.rotation.eulerAngles;

    transform.Rotate (rotChange);

So long as I'm keeping all the rotation within +- 30 degrees. When the plane is doing a barrel roll, it updates each step with the following:

    velocity = new Vector3 (barrelRollDirections.x * barrelRollXSpeed,        
                barrelRollDirections.y * barrelRollYSpeed, 0);

    roll += barrelRollTurnSpeed * Time.deltaTime;
    if (roll < -(fullRotation/2)) {
        roll += fullRotation;
    }
    if (roll > (fullRotation/2)) {
        roll -= fullRotation;
    }

The funny thing is that spinning the plane around actually works properly if the plane isn't moving along the x or y axis when I do it. But if it is, it jumps all over the place and only reappears in the proper final position once the roll is over.

I tried a number of ways to fix this problem:

-Convert my desired orientation to a quaternion using Quaternion.Euler and assigning that directly to transform.rotate. This didn't solve anything, and it also causes the plane to jitter and shake while at the bounds of its movement.

-Save the initial orientation of the plane in a quaternion, then, every update step, reassign that initial rotation to the plane and THEN call Transform.Rotate(new Vector3(-pitch,0,-roll)). I thought this would solve my problem if it was something like gimbal lock, since then I would just, every step, be applying rotations that were less than 180 degrees. But no dice.

1

1 Answers

0
votes

EDIT: I found it! After asking this question, I figured I'd go work on different objects since I was waiting for an answer. There I discovered that the transform.Translate function moves objects in a different direction based on their rotation. When I changed to simply reassigning the position, it worked fine. As per usual, the act of talking to someone else helped to resolve the issue.