0
\$\begingroup\$

Is there a way of changing the start/end proprieties (Transform, Rect Transform, etc..) of an animation via script? Like, I have 5 objects that start at the same position but should end in different positions, so I would like to change the end positions of each one via script.

As I haven't found a way to do this, I created 5 animations for this, but I'd like to just have one animation and change its proprieties in runtime.

\$\endgroup\$
  • \$\begingroup\$ It sounds like you might be able to replace this animation with a script that moves the objects to calculated positions, with the endpoint as a parameter. Can you describe the motion you want to perform? \$\endgroup\$ – DMGregory May 12 '18 at 16:49
  • \$\begingroup\$ In this case, I want to translate a Rect Transform from point X1 to X2, making each object have a different X2. In my scene I have other movements similiar but to other endpoints, so I wanted to make them all via script. \$\endgroup\$ – Daniel Guiomarino May 13 '18 at 15:05
1
\$\begingroup\$

I'm not sure if that's possible to be honest, but if you want something completely dynamic, why not make a script that handles the animation itself?

Here's an example:

public Vector3 startPosition;
public Vector3 endPosition;

float counter = 1f;

public Update() {

    if (counter <= 0f) {

        // Replace 1.0f with another number to change animation's speed
        counter -= 1.0f -Time.deltaTime; 

        /* Replace `position` with `localPosition`, if you would 
         * like your animation to take place in local space, 
         * instead of world space
         *
         * This is doing linear interpolation, if you would like to do
         * something more fancy, you can tweak `counter`, search for
         * "easing functions" for more info
         */
        transform.position = Vector3.Lerp(startPosition, endPosition, counter);

    }

}

public Animate() { counter = 0f; }

Add this in a class, make sure to add additional data like rotation or scale (if you want). Attach the script to any object that you want to animate, then change the startPosition and endPosition from the Inspector (or from a script, if the object is also made dynamically) for each individual object. Even though all objects are doing the same animation, they are moving to different locations.

Edit:

To support RectTransform you use the same logic, but you have to play with its anchor, pivot and position. You can read Unity's RectTransform API, but also make sure to read Unity: Designing UI for Multiple Resolutions, which can help you understand which specific value to animate and why (to appear properly on different screen sizes).

\$\endgroup\$
  • \$\begingroup\$ Would this approach also work with a Rect Tranform? Like, my objects are instantiated in my canvas and the animation occurs. Aside that this will help me with my objects outside the canvas. \$\endgroup\$ – Daniel Guiomarino May 13 '18 at 15:09
  • \$\begingroup\$ @DanielGuiomarino I edited my answer with more info about RectTransform \$\endgroup\$ – TomTsagk May 13 '18 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.