Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Cannot set weight of individual sources in Position Constraint

Discussion in 'Animation' started by sacb0y, Feb 15, 2020.

  1. sacb0y

    sacb0y

    Joined:
    May 9, 2016
    Posts:
    274
    Vincent13122 likes this.
  2. sacb0y

    sacb0y

    Joined:
    May 9, 2016
    Posts:
    274
  3. sacb0y

    sacb0y

    Joined:
    May 9, 2016
    Posts:
    274
    Actually no I can't get this to work at all :(

    I tried making a helper for bolt, but I can't get the values to change. Attaching script to respective objects.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Animations;
    5.  
    6. public class ConstraintHelper : MonoBehaviour
    7. {
    8.     private PositionConstraint positionConstraint;
    9.     private RotationConstraint rotationConstraint;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         positionConstraint = gameObject.GetComponent<PositionConstraint>();
    15.         rotationConstraint = gameObject.GetComponent<RotationConstraint>();
    16.     }
    17.  
    18.     public void PositionSourcesWeight(float newWeight, int sourceIndex)
    19.     {
    20.         ConstraintSource source;
    21.         source = positionConstraint.GetSource(sourceIndex);
    22.         source.weight = newWeight;
    23.     }
    24.  
    25.     public void RotationSourcesWeight(float newWeight, int sourceIndex)
    26.     {
    27.         ConstraintSource source;
    28.         source = rotationConstraint.GetSource(sourceIndex);
    29.         source.weight = newWeight;
    30.     }
    31. }
    What am I doing wrong?

    The weight values refuse to change.
     
  4. fcloss

    fcloss

    Joined:
    Dec 1, 2011
    Posts:
    129
    Exactly the same with me. It seems not to be working programmatically =Z

    I am using 2018.4LTS

    Anyone from Unity?
     
  5. fcloss

    fcloss

    Joined:
    Dec 1, 2011
    Posts:
    129
    My solution was to remove sources, recreate with correct weight and then add again.
     
  6. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,439
    Yeah I don't get why this is so hard to work with,

    Code (CSharp):
    1.  
    2. public void ChangeSourceWeight(ParentConstraint parentConstraint, int source, float newWeight)
    3. {
    4. ConstraintSource tempSource = parentConstraint.GetSource(source);
    5. tempSource.weight = newWeight;
    6. parentConstraint.SetSource(source, tempSource);
    7. }
    8.  
    I ended up using this.
     
    GameHourStudio and Vincent13122 like this.
  7. Vincent13122

    Vincent13122

    Joined:
    Oct 26, 2014
    Posts:
    76
    Thanks, its really weird that we have to use workarounds like that
     
  8. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    1,192
    ConstraintSource is a struct, meaning it gets copied by value and changes to the copy won't affect the original.

    It's the same reason you can't set
    transform.position.x = 0;
    . Vector3 is also a struct so that would get a copy of the position and modify the copy instead of modifying the actual position of the object. That's just the way C# works.
     
    Vincent13122 and petey like this.
  9. GameHourStudio

    GameHourStudio

    Joined:
    Apr 17, 2020
    Posts:
    37
    Thanks you so much. Setting weight of the source before adding to the source list solved my problem.
     
    petey likes this.
unityunity