Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Lerping between turn and fade phases of a disc golf disc

Discussion in 'Physics' started by Tchotchke9366, Nov 9, 2021.

  1. Tchotchke9366

    Tchotchke9366

    Joined:
    Jan 24, 2021
    Posts:
    7
    I am working on approximating the physics involved with a disc golf disc and I'm having trouble transitioning from the turn phase to the fade phase of the disc's flight. Assume Right Hand/Back Hand (RHBH) throws.

    In FixedUpdate I apply a leftward/rightward force to the disc, based on the turn and fade values of the disc. So if a disc has a turn rating of -2 and a fade rating of 1 I would apply a rightward force of the abs of the turn rating, and then a negative (leftward) force of the fade rating, based on the Y velocity of the disc's rigidbody.

    In code it looks like this:

    Code (CSharp):
    1.  
    2. disc = this.gameObject;
    3. discRB = disc.GetComponent<Rigidbody>();
    4.            
    5. // Turn and fade ratings are pulled from values I can modify in the editor.
    6. fadeVelocity = new Vector3(-fadeRating, 0f, 0f);
    7. turnVelocity = new Vector3(turnRating, 0f, 0f);
    8.  
    9. var _currentVelocityY = discRB.velocity.y;
    10.  
    11. /* While the Y velocity is sufficiently large, apply the turning effect as a force to the disc.  When Y velocity gets closer to zero, begin lerping between the turn and fade force values.  Finally, apply the fade effect as the Y velocity become larger in the negative direction. */
    12. if (_currentVelocityY > 2.5f)
    13. {
    14.     discRB.AddRelativeForce(turnVelocity);
    15. }
    16. else if (_currentVelocityY >= -2.5f && _currentVelocityY <= 2.5f)
    17. {
    18.     lerpVelocity = Vector3.Lerp(turnVelocity, fadeVelocity, 0.4f);
    19.     discRB.AddRelativeForce(-lerpVelocity);
    20.     fixedFrameCount++;
    21. }
    22. else if (_currentVelocityY < -2.5f)
    23. {
    24.     discRB.AddRelativeForce(fadeVelocity * (1f + fadeFrameCount / 50f));
    25.     fadeFrameCount++;
    26.     Debug.Log(fadeFrameCount);
    27. }
    28.  
    The issue I'm having is that the transition between turn and fade is rather abrupt, rather than gradual. I'm certain it's to do with the Vector3.Lerp t value, I'm just not sure how to go about modifying this to slow the turn effect while ramping up the fade effect.

    Am I using Lerp correctly here? And, what could I do to smooth out the transition between the turn and fade phases?
     
  2. Tchotchke9366

    Tchotchke9366

    Joined:
    Jan 24, 2021
    Posts:
    7
    In case anybody finds it helpful going forward, I kept looking for a solution, and came up with the code below. I've still got some work to do in regards to smoothing out that transition but this looks much better than it did before.

    Code (CSharp):
    1.  
    2. var _currentVelocityY = discRB.velocity.y;
    3. if (_currentVelocityY > 2.5f)
    4. {
    5.     // Lerp the velocity back toward a zero value.
    6.     var _turnLerpFloat = Mathf.Lerp(turnRating, 0f, tTurnLerp);
    7.     tTurnLerp += 0.5f * Time.fixedDeltaTime;
    8.  
    9.     var _applyForce = (turnRating - _turnLerpFloat);
    10.     discRB.AddRelativeForce(_applyForce, 0, 0);
    11. }
    12. else if (_currentVelocityY <= 2.5f)
    13. {
    14.     // Lerp the velocity toward the fade rating.
    15.     var _fadeLerpFloat = Mathf.Lerp(discRB.velocity.x, -fadeRating, tFadeLerp);
    16.     tFadeLerp += 0.5f * Time.fixedDeltaTime;
    17.  
    18.     var _applyForce = -(fadeRating - _fadeLerpFloat);
    19.     discRB.AddRelativeForce(_applyForce, 0f, 0f);
    20. }
    21.  
     
    Last edited: Nov 16, 2021
  3. JaytheADHDcoder

    JaytheADHDcoder

    Joined:
    Mar 11, 2023
    Posts:
    1
    so this is for right handed? Or can it do forehand shot calculation?