Fly Through (Rotate Ship With Position & Throw)

Video: https://youtu.be/GckP1MdTYzk

Ran into the same problem as many with the new Input System not allowing for smooth movement out-of-the-gate. My solution was to calculate the min and max pitch (or roll) and interpolate between the 2. Code below. Note xMove and yMove are just what I called xThrow and yThrow.

    float xMove = 0.0f, yMove = 0.0f;
    float pitchSmoothing = 0.5f, rollSmoothing = 0.5f;

    private void Rotate() 
    {

        // calculate rotations based on position
        float positionPitch = -transform.localPosition.y * positionRotateFactor;
        float positionYaw = transform.localPosition.x * positionRotateFactor;
        float positionRoll = 0.0f;

        // calculate rotations based on control
        float minControlPitch = -controlRotateFactor;
        float minControlRoll = -controlRotateFactor;
        
        float maxControlPitch = controlRotateFactor;
        float maxControlRoll = controlRotateFactor;

        pitchSmoothing = processSmoothing(yMove, pitchSmoothing);
        rollSmoothing = processSmoothing(xMove, rollSmoothing);

        float controlPitch = Mathf.Lerp(minControlPitch, maxControlPitch, pitchSmoothing);
        float controlYaw = 0.0f;
        float controlRoll = Mathf.Lerp(minControlRoll, maxControlRoll, rollSmoothing);

        // apply rotations
        transform.localRotation = Quaternion.Euler(
            positionPitch - controlPitch, 
            positionYaw - controlYaw, 
            positionRoll - controlRoll);
    }

    private float processSmoothing(float control, float curSmoothing) {
        float deltaSmoothingSpeed = smoothingSpeed * Time.deltaTime;
        if (curSmoothing > 0.5f) {
            if (control > 0.0f) {
                curSmoothing = Mathf.Min(curSmoothing + deltaSmoothingSpeed, 1.0f);
            } else if (control < 0.0f) {
                curSmoothing = curSmoothing - 2.0f * deltaSmoothingSpeed;
            } else {
                curSmoothing = Mathf.Max(curSmoothing - deltaSmoothingSpeed, 0.5f);
            }
        } else if (curSmoothing < 0.5f) {
            if (control > 0.0f) {
                curSmoothing = curSmoothing + 2.0f * deltaSmoothingSpeed;
            } else if (control < 0.0f) {
                curSmoothing = Mathf.Max(curSmoothing - deltaSmoothingSpeed, 0.0f);
            } else {
                curSmoothing = Mathf.Min(curSmoothing + deltaSmoothingSpeed, 0.5f);
            }
        } else {
            if (control > 0.0f) {
                curSmoothing = Mathf.Min(curSmoothing + deltaSmoothingSpeed, 1.0f);
            } else if (control < 0.0f) {
                curSmoothing = Mathf.Max(curSmoothing - deltaSmoothingSpeed, 0.0f);
            }            
        }
        return curSmoothing;
    }

2 Likes

Awesome job solving the problem you ran into.

1 Like

Privacy & Terms