Search Unity

How do I make a script modify an animation?

Discussion in 'Scripting' started by ecnalyr, May 31, 2010.

  1. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    I've posted the exact details of my situation on Unity Answers, but haven't had any luck getting an answer see (http://answers.unity3d.com/question...n-animated-bubble-to-blow-up-to-a-random-size)

    I was wondering if anyone here knew how I would have a script modify an animation? I do not know what command I would give to have the animation (for example) change the x,y,z scale of an object from 1,1,1 to 3,3,3 over a 2 second time period.

    Is it possible to do that? If so, how?

    Edit:

    What if I were instead to try to use Transform.Scale as Maltus suggested on Unity Answers. . .

    How would I go about doing something like:

    <code><pre>
    for random(min, max) seconds; (the duration it would take to complete the next line)
    Change size to random(min, max) Scale;
    </pre></code>

    Could the above be coded some how?
     
  2. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    Is it simply not possible to modify an animation via a script in Unity?

    If so, would it be possible to write an animation in a 3d modeling program and then modify it's parameters via a script in Unity?
     
  3. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    You can do that, but it would be incredibly difficult.

    You would have to go to the Animation Clip, clear the curves, and create a new curve from scratch, inserting a new list of keyframes.

    Otherwise you could just modify transform.scale;

    All that the animation clip does is modify those values along the curve listed, and you could mimic that within an update loop.

    i.e.
    Update
    {
    transform.scale += Vector3.One * (size/speed);
    }

    That is really simple/base pseudo code, but it would mimic a linear animation. you would need someway to cap it once it hits it's target.

    Getting the Ease in and Ease out Properties could be difficult to mimic, but trying to get the animation curve from the Animation Clip (that's within the Animation Script) and then modifying the appropriate keys would be no less difficult.
     
  4. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    Ok, based on what information you suggested I've got the script up to this:
    Code (csharp):
    1.  
    2. var startScale = Vector3.zero;
    3. var endScaleMax = Vector3(5, 5, 5);
    4. var endScaleMin = Vector3.one;
    5. var t = 0.0;
    6. var maxspeed = 1;
    7. var minspeed = .1;
    8.  
    9. while (t<1.0) {
    10.     var speed = (Random.Range(minspeed, maxspeed));
    11.     t += Time.deltaTime * speed;
    12.     transform.localScale = Vector3.Lerp(startScale, Random.Range(endScaleMin,endScaleMax), t);
    13.     yield;
    14. }
    15.  
    This has two problems,

    1: It throws the error: "(11,69): BCE0023: No appropriate version of 'UnityEngine.Random.Range' for the argument list '(UnityEngine.Vector3, UnityEngine.Vector3)' was found."

    How do I resolve this error whilst maintaining a random maximum Vector3?

    2: If I take the above problem line out and set it to say just "endScaleMax", my objects spawn, but not at a random speed.

    How do I make it so that each time one of my gameObjects is spawned it has a random speed?
     
  5. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    Random.Range works for floats not vectors. If you wanted to create a vector with 3 random values you would need to generate a random number for each coordinate.
     
  6. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    Aha! *facepalm* Thanks.
     
  7. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    Still fiddling with it,

    So far I have this :

    Code (csharp):
    1.  
    2. var startScale = Vector3.zero;
    3. var endScaleMax = Vector3(5, 5, 5);
    4. var endScaleMin = Vector3.one;
    5. var t = 0.0;
    6. var maxspeed = 1;
    7. var minspeed = .1;
    8. var minscale = 1;
    9. var maxscale = 5;
    10.  
    11.  
    12. while (t<1.0) {
    13.     var superz = Random.Range(minscale,maxscale);
    14.     var randomgen = Vector3(superz,superz,superz);
    15.     var speed = (Random.Range(minspeed, maxspeed));
    16.     t += Time.deltaTime * speed;
    17.     transform.localScale = Vector3.Lerp(startScale, randomgen, t);
    18.     yield;
    19. }
    20.  
    Now this is not doing what I want because it generates the random values for randomgen (the max size my objects will grow) during the while loop - it makes the objects abruptly change size randomly while they are growing.

    What would be the best way to move this out of the while loop?

    Would I want to use a static variable for randomgen and have the script that spawns the object generate that value and pass it along to the spawned object?

    Would this make each spawned object have it's own randomgen value, or would they still all get the same random numbers pushed into them constantly until they finish spawning?

    Would it be plausible to sent a value during the Instantiate function?

    Currently my Instantiate looks like this:
    Code (csharp):
    1.  
    2. Instantiate(Prefabobject, randomPositionvariable, Quaternion.identity);
    3.  
    Could I somehow put something into that code above that would push the 'randomgen' value into the Instantiated object?

    I've changed this drastically, still not having it function as I intend, but it is so different I'm going to make a different post.