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.

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:
    57
    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:
    57
    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.