Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved [Splines] How to move a knot from code (effectively)?

Discussion in 'World Building' started by icauroboros, Oct 20, 2022.

  1. icauroboros

    icauroboros

    Joined:
    Apr 30, 2021
    Posts:
    123
    I want move first knot of a spline to target transforms position. This code works nicely:

    Edit : this works only if target transform child of spline object, see my other post for real solution.

    Code (CSharp):
    1. var firstKnot = MySpline.Spline.ToArray()[0];
    2. firstKnot.Position = TargetTransform.position;
    3. MySpline.Spline.SetKnot(0,firstKnot);
    But seems too much for just a float3 change. Is there way to handle like this ?

    Code (CSharp):
    1. MySpline.Spline.Knots[0].position = TargetTransform.position;
    I looked documentation but maybe I missed something.
     
    Last edited: May 2, 2023
  2. mush555

    mush555

    Joined:
    Feb 4, 2020
    Posts:
    23
    I have the same problem.
    Does anyone know?
     
  3. icauroboros

    icauroboros

    Joined:
    Apr 30, 2021
    Posts:
    123
    If you want to move a spline to the target world position and rotation,

    Code (CSharp):
    1.   public SplineContainer MySpline;
    2.     public Transform KnotTarget;
    3.  
    4.     private void Update()
    5.     {
    6.         var firstKnot = MySpline.Spline.ToArray()[0];
    7.  
    8.         firstKnot.Position = MySpline.transform.InverseTransformPoint(KnotTarget.position);
    9.         firstKnot.Rotation = Quaternion.Inverse(MySpline.transform.rotation) * KnotTarget.rotation;
    10.  
    11.         MySpline.Spline.SetKnot(0,firstKnot);
    12.  
    13.     }
    with that first knot always copy target transform world position and rotation even spline itself move or rotate.
    Just like a Parent constraint component.
     
    GDevTeam likes this.
  4. airburst_studios

    airburst_studios

    Joined:
    Jan 7, 2023
    Posts:
    36
    Isn't `MySpline.Spline.ToArray()[0];` assigning the first spline in your SplineContainer instead of a Knot in a Spline?
     
  5. agusmazoalarza

    agusmazoalarza

    Joined:
    May 15, 2021
    Posts:
    4
    Yep, same thought, did you figure it out by any chance?

    I mean, the code works but I still dont know how after checking for a while lol
     
  6. icauroboros

    icauroboros

    Joined:
    Apr 30, 2021
    Posts:
    123
    Read the api correctly, SplineContainer.Spline returns the main Spline. Then Spline.ToArray returns the array of knots.
    And then get knot you want to use by indexer, like "[0]" if you want to move first spline.