Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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:
    867
    Vincent454 likes this.
  2. sacb0y

    sacb0y

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

    sacb0y

    Joined:
    May 9, 2016
    Posts:
    867
    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:
    192
    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:
    192
    My solution was to remove sources, recreate with correct weight and then add again.
     
  6. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,817
    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.
     
    Fangh, WaqasGameDev and Vincent454 like this.
  7. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,554
    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.
     
    Vincent454 and petey like this.
  8. WaqasGameDev

    WaqasGameDev

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

    Barada

    Joined:
    Apr 27, 2015
    Posts:
    99
    using 2019.4,0f1 you need to add constraint source in you bolt unit setup and then set a temp source variable similar to posted code upload_2020-11-25_12-27-21.png