Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

New spline-package: auto-calculate tangents for each knot

Discussion in 'Scripting' started by Feuerputz, Apr 27, 2022.

  1. Feuerputz

    Feuerputz

    Joined:
    Sep 6, 2017
    Posts:
    43
    Hello,
    currently I try getting my feet wet with the new Unity Spline package. In the editor it's simple to use: click to create the knots, that my spline should consists of and based on the EditType, it will automatically create a smooth curve by setting the tangentIn and tangentOut fields for me.
    upload_2022-4-27_12-47-11.png

    Now to my question: I successfully created a spline via code and set their knot-positions, but no matter which EditType I choose, it remains a bunch of straight lines. See picture below, where I sampled positions from the spline and drew some gizmos:
    upload_2022-4-27_12-53-10.png

    When I set the tangentIn, tangentOut fields, it ovbiously becomes a smooth curve.
    However: Is it possible to auto-calculate these tangentIn, tangentOut field, like the editor-tool seems to be capable of doing?
    I've checked the Unity API and Manual, but could not find any information on this topic. Probably missed something.

    Maybe anyone has some knowledge regarding this topic. Thanks in advance!
     
    mishakozlov74 likes this.
  2. mishakozlov74

    mishakozlov74

    Joined:
    Aug 8, 2018
    Posts:
    135
    Yep, have the same question. When I change value in editor, it works, but I can't achieve the same effect via code.

    I'm currently looking into the implementation of the package, maybe find something.
     
  3. Feuerputz

    Feuerputz

    Joined:
    Sep 6, 2017
    Posts:
    43
    I went on calculating the tangents by myself in the end. Basically it comes down to calculating the vector_A from the incoming knot to the current knot and vector_B from the current to the next knot. Then averaging these and calculating the position of tangentOut; then tangentIn basically reversed.

    Good luck!
     
    mishakozlov74 likes this.
  4. sasos90

    sasos90

    Joined:
    Jul 12, 2022
    Posts:
    1
    @Feuerputz I am using Sprite Shape Controller but having basically the same problem. Can you elaborate on calculating the tangents please?
    Thanks a lot!
     
  5. arcusdavid

    arcusdavid

    Joined:
    Jul 28, 2020
    Posts:
    1
    The Spline package has some examples that demonstrate auto-smoothing of splines. If you go to the package manager, search for Splines then click the 'Samples' tab there's an option to import them.

    Looking at the Path fitting example Unity provide you can auto fit your knot using the following:

    Code (CSharp):
    1. var all = new SplineRange(0, spline.Count);
    2.  
    3. // Sets the tangent mode for all knots in the spline to "Auto Smooth."
    4. spline.SetTangentMode(all, TangentMode.AutoSmooth);
     
    morepixels likes this.