2
\$\begingroup\$

In my Unity project I have a script that programmatically changes the scaling of an object during the game.

Recently I wanted to add a spawning animation clip to my object, which modifies the object's scaling (but only during a short delay). So I added an animation controller to the object, containing the spawning animation clip.

I can now successfully run the spawning animation but the in-game programmatic scaling changes don't work anymore, even when my animation is not running.

Any idea?

\$\endgroup\$
4
  • \$\begingroup\$ Probably because it's being overridden by the scaling animation... Unless you are talking about changing the scaling when the animation isn't playing? If that is the case then we may need to see your code. \$\endgroup\$
    – Savlon
    Mar 22, 2015 at 11:48
  • \$\begingroup\$ Yes, my script attempts to modify the scaling property when the animation isn't playing, but that doesn't work. There's not much code to show actually, just some gameObject.transform.scaling = ... in the Update() method \$\endgroup\$
    – sdabet
    Apr 22, 2015 at 7:49
  • \$\begingroup\$ I agree with Savlon, it's probably the animation that keeps scaling the object, thus preventing the other script from working on it. If you could share both the important parts of the scripts, it would help us help you. \$\endgroup\$
    – Zee
    Apr 22, 2015 at 8:40
  • \$\begingroup\$ How about encapsulating the object that has your mesh renderer into another object, and set the scaling for the parent object instead? \$\endgroup\$ Apr 22, 2015 at 12:34

4 Answers 4

1
+50
\$\begingroup\$

I think you have to move all procedural animation to LateUpdate(). This will allow you to override Unity animation.

UPD: and you have to animate lossyScale instead on localScale, since localScale is animated by Unity (look for Mitchell's answer for details)

\$\endgroup\$
1
  • \$\begingroup\$ Moving my code to LateUpdate() actually did the trick. Thanks. \$\endgroup\$
    – sdabet
    Apr 23, 2015 at 9:44
4
\$\begingroup\$

As others have pointed, your animation is most likely overwriting the transform of your GameObject.

Generally, you can't (cleanly) have both an animation and a script modify the transform of the same GameObject without conflicting between each other.

So how about, instead of fighting against Unity to get an animation and a script to modify the same transform, you put the GameObject that has the MeshRenderer inside another object.

This way, the animation will affect the child object, the script will affect the parent object, and you get the benefit of both transforms at the same time.

\$\endgroup\$
1
\$\begingroup\$

The clean way is to add a new property, eg called animatedScale, to your script which you animate instead of directly animating the object's scale. then, in your Update, you set the gameobjects scale to 'animatedScale * yourScale' or whatever you want. This way the animation and your scaling are seperated and applied in a controlled way.

\$\endgroup\$
0
\$\begingroup\$

Somewhere in your code you're still setting the scale, so each time you try to modify the scale in the editor when running the game, it resets the scale. Without being able to look at your code it's impossible to tell you what is exactly causing the problem.

In addition to that, when you are running an animation it overwrites the localScale, so you can't possibly combine an animation that changes the scale and programmatical scaling. Basically, animation clips can only have one scale (or none).

So you might want to consider replacing your animation clip with programmatical animation entirely.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .