How to smoothen the controls of the ship and/or the animation

Hello

I wanted to deactivate pitch- and roll-rotation when the ship is at the x- and y-borders. I think this shows the player more clearly that he can’t move any more left/right or up/down.
Therefore I adjusted my code for ProcessRotation, so pitch is only executed when the local y-position is between the border values and likewise for roll and the x-position.
So it looks like this:

void ProcessRotation()
{
    float xBorder = Mathf.Abs(transform.localPosition.x);
    float yBorder = Mathf.Abs(transform.localPosition.y);

    if(xBorder < 10 && yBorder < 5)
    {
        float pitchDueToPosition = transform.localPosition.y * pitchFactor;
        float pitchDueToControlThrow = yThrow * controlPitchFactor;
        pitch = pitchDueToPosition + pitchDueToControlThrow;

        roll = xThrow * rollFactor;

        yaw = transform.localPosition.x * yawFactor;

        transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
    }

    else
    {

        mildYaw = transform.localPosition.x * mildYawFactor;
        transform.localRotation = Quaternion.Euler(0, mildYaw, 0);
    }
}

The good thing is that my little extension works. But I would like to further improve my code and smoothen the transitions.

Are there any suggestions on how to slowly translate between the current rotation-value and 0?
It’s especially obvious when the ship reaches the y border, then the x-rotation snaps from somewhere around ± 20 to 0

Many thanks in advance!

Thomas

Hi Thomas,

Good job on making your idea work. The only “issue” I see are the hard-coded values in the if-condition. Maybe you could replace them with variables.

Regarding your problem, have you already tried to add Debug.Logs to your code to see what is going on during runtime? Maybe you could figure out why the snapping happens around ±20.


See also:

Hi Nina,

thanks for your feedback!
You’re right about the hard-coded zeros in the else-statement. They bother me too ^^ so I’ll replace them with variables. I hope Debug.Logs help me to find what causes the snapping…but if not, it’s no big deal.

Best,
Thomas

In my opinion, the hard-coded 0s are fine because the idea is clear: You want to rotate around a specific axis only. Unless Unity change their source code, the 0s will always be 0s.

I was referring to: if(xBorder < 10 && yBorder < 5). The border values might change, and in that case, you would have to look for those hard-coded values somewhere in your code.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms