Search Unity

Using AnimationClip.SetCurve on child object.

Discussion in 'Scripting' started by Uran, Jul 20, 2014.

  1. Uran

    Uran

    Joined:
    Jul 22, 2012
    Posts:
    8
    If i have 2 cubes, the first is the parent of the second in the hierarchy. How do I aim the relativePath parameter at the child cube?

    Here is what I have tried, the code is in a script attached to the parent cube.
    Code (CSharp):
    1. private void Test()
    2. {
    3.     AnimationClip clip = new AnimationClip();
    4.     AnimationCurve curve = new AnimationCurve.Linear(0, 1, 2, 3);
    5.  
    6.     clip.SetCurve("", typeof(Transform), "localPosition.x", curve);
    7.     clip.SetCurve("Child", typeof(Transform), "localEulerAngles.x", curve);
    8.  
    9.     animation.AddClip(clip, "test");
    10.  
    11.     animation.Play("test");
    12. }
    Obviously, this doesn't work. The reference(link), does not really shed any light on how to use the relativePath parameter.
     
  2. Dave-Hampson

    Dave-Hampson

    Unity Technologies

    Joined:
    Jan 2, 2014
    Posts:
    150
    What are you trying to do exactly? Just get the GameObject of the second (child) cube and then add an animation curve onto it so it moves around?
     
  3. Uran

    Uran

    Joined:
    Jul 22, 2012
    Posts:
    8
    Well, I'm currently implementing a importer for a custom 3d model format, which in turn enables modding. What I'm trying to achieve here is that I have one animation clip that manipulates the children of the object that the clip is assigned to.

    Hope this clarifies what I intended with this piece of code. Thank you for your time.
     
  4. gridside

    gridside

    Joined:
    Jul 26, 2013
    Posts:
    97
    Try this: (not tested)

    Code (CSharp):
    1. public Transform childCube;
    2. private void Test()
    3. {
    4.    AnimationClip clip = new AnimationClip();
    5.    AnimationCurve curve = new AnimationCurve.Linear(0, 1, 2, 3);
    6.  
    7.    clip.SetCurve("", typeof(Transform), "localPosition.x", curve);
    8.    clip.SetCurve(childCube.name, typeof(Transform), "localEulerAngles.x", curve);
    9.  
    10.     animation.AddClip(clip, "test");
    11.  
    12.     animation.Play("test");
    13. }