var data = aim.data.sourceObjects;
data.SetTransform(0, PlayerController.instance.aimTransform);
aim.data.sourceObjects = data;
Here is my code, but I’ve tried with sourceObjects.Add() too. The thing is, I am seeing my new source in editor, but it is completely ignored by constraint.
I am using 0.2.7 package and Unity 2019.4
Please help
Yes, it’s a common issue that comes up when setting sources at runtime. The rigbuilder animation graph is already built by the time you change the source objects so that won’t work by itself.
Does setting weight only works in Update() Method. I tried a callback method for setting weights but it didn’t sets the weight on the Constraint. When I copied the whole block into Update() method, it worked
The Constraints and Rigs weights are animatable properties that are bound to the Animator. However, to make sure they are updated with scene values when they are not directly animated by a clip, we sync the scene values in the animation stream during animation evaluation (ProcessAnimation).
What this means is that if you change the weight after the value has been synced, you will most likely lose your modifications after the animation system has finished writing back to scene.
Therefore, the best entry point where you should do weight changes is in the Update function before the animation system evaluates.
I’ve just run into this same issue (at least I think it is the same). I have a Prefab that I need to set aim constraints on the Text object contained within. I want the text to always stay oriented to the camera. This was working fine until I made the composite GameObject a Prefab. This lost the connections to the camera.
Here is what I’ve tried.
galacticAxesObj = (GameObject) Instantiate(galacticAxesPrefab);
// Need to set the aim constraint of all the labels
var comps = galacticAxesObj.GetComponentsInChildren<AimConstraint>();
foreach (var comp in comps)
{
comp.worldUpObject = camera.transform;
var contraintSource = new ConstraintSource {sourceTransform = camera.transform};
comp.SetSource(0, contraintSource);
}
It seems like this should be working but it has no effect. I am not using animations here so I don’t understand how the RigBuilder would come into play. Is there something else I need to be calling?
Yeah I was thinking that might be it but I can’t even get it to work on a simple box setup.
I’m going to have another try today if I can get a simple demo together I’ll post it up.
I realize this is a fairly old post but I realize that there really aren’t any good Scripting Api documented examples for setting source objects at runtime. The few I’ve seen in the forum tend to use a copy of the component itself and more than one line to do it. Surely there’s a simpler way, right? I mean must it always be var this and that?
Anyway here’s my solution:
The following is specifically for Animation Rigging version (1.0.3) though you may get mileage out of it as well
First define and name your target gameObject(s).
Then when you’re ready to assign your target to your rig’s MultiAimConstraint child sourceObjects?
transformchild.GetComponent<MultiAimConstraint>().data.sourceObjects = new WeightedTransformArray{new WeightedTransform(your target's transform,1*)};
*this number represents its weight of influence 1 being the highest influence and 0 being none at all.
Despite the fact that in the editor it looks like a list I came to a realization. Its not a list its an array. How would you define an array?
You still want to do all the other stuff suggested previously though. But at least setting the source Objects should be simpler now.
A simple solution that I just found was to create an empty object as a child of the object that you want to control the aim for. Call it “aim_target” and assign that as the source in the inspector. Simply change the aim_target’s transform.position at runtime from for instance “nearest_enemy.transform.position” to “object_of_interest.transform.position”… I’m satisfied with it as a simple work-around.