Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Playing Animation Local to Position in Unity

Discussion in 'Editor & General Support' started by barrelmaker84, Jul 9, 2010.

  1. barrelmaker84

    barrelmaker84

    Joined:
    Apr 3, 2010
    Posts:
    4
    Hey all, I'm just now getting started with importing animations into Unity, and I have what will probably end up being a really basic question.

    So I have a simple character, non-deforming. I'm animating him to bob up and down very slowly along the positive y axis in Maya, so the animation goes from like 0 to 1 on y and then back down again. Now in Unity, this animation comes in fine. However, my character is positioned away from the origin in Unity, and whenever I hit the play button to preview the animation, the character snaps back to the origin (since that was where the y translation was keyed in Maya).

    My question is this: is there any way to say "Hey Unity, I want to play this animation local to where the character is currently positioned"? In other words, if the character is positioned off-origin in the Unity game-world, is there a way to have him animate in place wherever he is positioned? Or maybe I need to go about the animations in Maya a different way?

    Thoughts? Thanks! :D
     
    vishu-kumar likes this.
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You just need to place the animated object inside an empty parent object. The animation will then be positioned in the parent's coordinate space, and you can move the parent to reposition the visible object.
     
  3. MattFS

    MattFS

    Joined:
    Jul 14, 2009
    Posts:
    219
    doesn't this seem a bit of a long winded approach? surely just being able to flag an animation as 'Local' would suffice and would be a trivial addition to animations? Perhaps in the AnimationState class?

    Even if parenting (say you have planets under a Sun parent) you may still want an animation to be absolute/global even though it's parented!
    For whatever reason, you may want the planets to bob up and down locally as they orbit... but then play a global space anim if you select them, forcing an absolute position... just one example!

    So letting users decide how the anim transforms are applied could open up more flexibility and do away with redundant busy work! :)
     
    Lightning_A and pointcache like this.
  4. Lonners

    Lonners

    Joined:
    Jul 15, 2010
    Posts:
    25
    Is there still no way to do this, i.e. for a weapon on a player character animation.

    Either that or can you script animations instead?
     
    sickshredzz likes this.
  5. Paulius-Liekis

    Paulius-Liekis

    Joined:
    Aug 31, 2009
    Posts:
    672
    It much easier to say that than to do it and keep proper backwards compatibility. It just doesn't fit current Unity design. The way it works: Animatoin component just overrides values in Transform - that how you get "world space" animation. What you're suggesting is that Animation should add values to Transform (that how you would get "local space animation), which means Animation component would have to store the original values somewhere and then if you want to change the "world space" on which animation is played you would have to change it not on Transform, but somewhere else where Animation component stored it, so it just becomes a mess. Does it make sense?

    So just parent your game object to empty game object, or add another root in your 3D file and do not animate it (then animation should not affect it).
     
    amoghsgk, Gamba04 and Funattic like this.
  6. shader

    shader

    Joined:
    Apr 4, 2009
    Posts:
    253
    Yes, this seems logical, just wish it was documented in the user manual !
     
  7. Charles-Van-Norman

    Charles-Van-Norman

    Joined:
    Aug 10, 2010
    Posts:
    86
    I also need to move animations around and have them keep their local movement. But when I parent 6 animated objects to an empty Transform, as soon as I push "Play" all my animated objects disappear!

    If I de-parent them from the empty Transform, they play as normal. What's going on!?
     
  8. Steven-Walker

    Steven-Walker

    Joined:
    Oct 27, 2010
    Posts:
    38
    This is a tricky topic and has taken me a while to figure out, though I did find a solution that may help others. You can use a script to offset an animation by applying the offset during LateUpdate. It won't work during Update because the transform values will get overwritten by the animation. As pointed out by Paulius, the animation clip simply sets values to the transform. You can add your own values to the transform afterward:

    Code (csharp):
    1. function LateUpdate() {
    2.     transform.localPosition.x += myOffset.x;
    3.     transform.localPosition.y += myOffset.y;
    4.     transform.localPosition.z += myOffset.z;
    5. }
     
  9. MattFS

    MattFS

    Joined:
    Jul 14, 2009
    Posts:
    219
    hey walkerfx, in your script, where did you define myOffset?
    I'll give that a shot!

    hey Paulius - it makes total sense what you say yet I don't see the problem, in principle at least, with how you described approaching the issue? What I suggested would still be backward compatible as well - the default setting for the new Local/Absolute enum (or however you implement it) would be what the normal behaviour in previous versions would have been.

    I understand of course that in practice modifying a component at the source level isn't as simple as "just" adding a new enum.

    Cheers! :)
     
  10. temp_account

    temp_account

    Joined:
    Apr 24, 2011
    Posts:
    6
    Hey there, just in case someone is struggling with this as well and finds this topic by googling, I managed to put together a very simple script file (in C#) that makes the animation of the object play locally:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(Animation))]
    4. public class LocalAnimation : MonoBehaviour
    5. {
    6.     Vector3 localPos;
    7.     bool wasPlaying;
    8.  
    9.     void Awake()
    10.     {
    11.         localPos = transform.position;
    12.         wasPlaying = false;
    13.     }
    14.  
    15.     void LateUpdate()
    16.     {
    17.         if (!animation.isPlaying  !wasPlaying)
    18.             return;
    19.  
    20.         transform.localPosition += localPos;
    21.  
    22.         wasPlaying = animation.isPlaying;
    23.     }
    24. }
    25.  
    It's very brute force but quite simple, all you need to do is to add it to your object that has the animation. It will check if the animation is playing and modify the position of the object accordingly.
     
    Colin_MacLeod and Dr_Who like this.
  11. spacefrog

    spacefrog

    Joined:
    Jun 14, 2009
    Posts:
    734
    What about introducing a new component type (or even subclass of "animation" ) called "LocalAnimation" ( or "AnimationLocal", or "AnimationAdditive" or something ) ? That surely would keep backward compatibility while give the user some comfort...

    If i re-think about it: why not just introduce a boolean property to the Animation component called "additive" or "local" or whatever, which is off by default ?
     
    TomTrottel, pointcache and juliuslaak like this.
  12. thesfid

    thesfid

    Joined:
    Oct 19, 2010
    Posts:
    12
    Just in case someone else runs into what I did, be sure to create your empty game object first. Then drag your model from the Project panel to the new empty game object in the Hierarchy panel.

    In my case, having the model already in the hierarchy panel > creating empty game object > moving model inside empty game object > same undesired result with model position jumping around.

    Hope this helps.

    JMc
     
    PappaLemmings likes this.
  13. smiljan66

    smiljan66

    Joined:
    Jun 9, 2010
    Posts:
    610
    Thank you oooo lord i was like 3 hours on that and it was just drawing fomr the project apnel and not into the hierarchy and then etc ...GOOD BLESS YOU MAN YOU ROCK !
     
    jenninexus likes this.
  14. kamullis

    kamullis

    Joined:
    Feb 7, 2013
    Posts:
    21
    I am having the same problem with jumping. I am using the free version of unity 4 but I am following a tutorial that was created with unity 3.5. Of course they don't talk about the animator controller in the tutorial but I was able to figure that out. So now the animation is running when I hit play.

    However I am having issues with the character jumping back to the origin. I have tried creating an empty game object and parenting the character to it. I even tried creating the empty game object then putting the character in it from the project window as suggested by thesfid.

    Any ideas why this might not be working for me?

    Thanks!
     
  15. Alf203

    Alf203

    Joined:
    Dec 7, 2012
    Posts:
    461
    Which tutorial is it ? You are using the legacy system, right ? If you are using Mecanim the parent game object trick will not work and its probably a different problem you are having.
     
  16. kamullis

    kamullis

    Joined:
    Feb 7, 2013
    Posts:
    21
    I'm doing the lynda.com unity 3.5 essential training

    Yeah I was trying to use the Mecanim Animator component. Which was giving me the "jumping to origin problem" even with parenting to an empty game object.

    I then tried the Legacy animation component. The character stayed put, but the animation doesn't play and I made sure to add the animation clips to the component.
     
    mariacotuna likes this.
  17. Alf203

    Alf203

    Joined:
    Dec 7, 2012
    Posts:
    461
    With mecanim there is similar trick to parenting but its a lot more tedious and needs to be done in the animation file and Unity. Since Legacy will be completely phased out by Mecanim in the future, it would be nice to be able to have a similar trick with mecanim to have animations played at local position on objects which don't have bones. Currently to animate a non-rigged object with mecanim and have them play locally they need to be inside two parents to make it work...

    Anyways about your problem on legacy, Did it give you any errors or warnings ? Are you using code to animate your character or you just put some animation on the animator component and had Play Automatically checked ?

    Make sure you set both the model and animations to Legacy in the Rigs tab : http://docs.unity3d.com/Documentation/Components/FBXImporter-Rig.html

    Its usually the most common mistake. The docs are your friend :p.
     
    Last edited: Feb 10, 2013
    jenninexus likes this.
  18. lockbox

    lockbox

    Joined:
    Feb 10, 2012
    Posts:
    519
    In legacy..

    For local animations, your child needs to be named properly inside of the animation, otherwise Unity ignores it. See my post about this...
    http://forum.unity3d.com/threads/16...AnimationClip.SetCurve-quot-relativePath-quot

    When the animation appears not to play, it's because the name of what is suppose to be animated does match the object name inside of the animation clip. When it jumps, it's because it's a global animation. Meaning the parent is the target of the animation.
     
  19. kamullis

    kamullis

    Joined:
    Feb 7, 2013
    Posts:
    21
    Thanks guys! I'll see if I can get your suggestions to work.
     
  20. typane

    typane

    Joined:
    Nov 25, 2011
    Posts:
    297
    I am stuck at this as well. In 3.5 the parenting trick is not working. I will keep searching around.
     
  21. eoghandee

    eoghandee

    Joined:
    Jan 6, 2013
    Posts:
    3
    Finally got this working, basically inside the prefab you must change the rig. If its generic then apply the root bone. Another option is if you change from Generic to Legacy under Animation type option. Now it works great. Spent all day getting it right, can relax now :D
     
    Last edited: Mar 14, 2013
  22. Sissi

    Sissi

    Joined:
    Mar 8, 2013
    Posts:
    3
    Hi, Can you say more clear about how to change rig,
    and where to change Generic to Legacy?
    Can you say more clear ?
    Thx so much!!!!!
     
  23. Sissi

    Sissi

    Joined:
    Mar 8, 2013
    Posts:
    3
    Sorry for anoying you guys~~ I hv fixed this problems.~~ Hv fun
     
  24. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,259
    Is this still the case in Unity 4.2? All animations are in world coordinates and there's no built-in way to make them local?

    I've just spend a couple of hours trying to learn Unity's animation system, and find it's pretty useless for moving a character if it only uses world coordinates. Is there no way to make an object move forward by 10 relative to it's current position (without a workaround)?
     
  25. Shinugami

    Shinugami

    Joined:
    Oct 17, 2012
    Posts:
    54
    Applying an Animator component to an object and having it play an animation, when the x,y,z coordinates are manipulated in the animation, despite all efforts the result is movement on a world axis.

    In 4.3, I've used parent objects.
    I even thought for a moment that animations are local to the axis of object with the animator component however applying multiple animator components still resulted in the position being manipulated on a world axis.

    Unity needs to include another set of axis in the animation parameters for local position. Not having this ability REALLY hinders game production for some people. In fact, it means game creators need to totally redesign their method of animation just to work with such an obstructive fact.

    Looking at what Unity is and why it was made, writing the animation for individual frames in code seems like a trip back to 1980s~1990s.
     
    Last edited: Dec 5, 2013
  26. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,910
    SOLUTION: Unity 5 can "Generate Root Motion Curves" for animations created from the Animation window.



    E.g. To do a cheap jump, you can create a jump animation on the Player's root GO and animate it using the animator, while your 3d model (a child GO), has the actual jump (on-the-spot) animation playing.
     
    Last edited: Mar 4, 2015
    mx3307, Shippety, domkia and 5 others like this.
  27. Phan-Nhung

    Phan-Nhung

    Joined:
    Dec 25, 2012
    Posts:
    15
    Hi andeeeee,
    I have this trouble too. In my case, I can play animation object at parent location. But the front view now is changed ( cause I used Lookat funtion ), and I don't know how to fix this.
    Please help me.
    Thank you
     
  28. AdeelAbbas

    AdeelAbbas

    Joined:
    Jul 18, 2014
    Posts:
    22
    Hi everyone
    Can someone please guide me I am using magnet power up which actually works on static gameobjects but not work on animated gameobjects.
    Thanks in advance
     
  29. Leuthil

    Leuthil

    Joined:
    Jul 26, 2013
    Posts:
    97
    I have been waiting for this forever. Not sure how I didn't see this in Unity 5 notes.
     
  30. WalterBoyd

    WalterBoyd

    Joined:
    Sep 25, 2015
    Posts:
    13
    I'm dealing with this issue now. I need to visually represent a drag-drop event. So my object needs to fly from one location in the scene to it's target, and both start and end points cannot be hard coded. Should I even try to use the animator for this?
     
  31. Shinugami

    Shinugami

    Joined:
    Oct 17, 2012
    Posts:
    54
    I would use Lerp to move from A to B. Use colliders OR a distance check to trigger an animation to play only once (if it's not already playing).

    So I would:
    1) Contain the animation in an object.
    2) Move that object
    3a) Use distance || colliders || an event to trigger the animation.
    3b) Use a LookAt() or similar script to make the object face the right direction while it's animation plays.

    Other advice:
    Whenever you want a mobile animated object or a pivot point, use an empty game object to hold the animated object.

    ===================
    Ah, so if it's drag and drop:
    1 - Have an animation trigger on input.GetButtonDown (whatever the button/key reference is)
    2 - have another animation trigger on input.GetButtonUp (whatever the button/key reference is)
     
    Last edited: Nov 12, 2015
  32. Amir-Khan

    Amir-Khan

    Joined:
    Jul 1, 2015
    Posts:
    3
  33. Shippety

    Shippety

    Joined:
    May 9, 2014
    Posts:
    31
    Stardog you legend, appreciate man
     
    Dayls and mx3307 like this.
  34. MincDev

    MincDev

    Joined:
    Oct 11, 2018
    Posts:
    2
    Yours is the only thing that actually worked and does not seem hacky. Geat!!!
     
  35. HernandoNJ

    HernandoNJ

    Joined:
    May 13, 2018
    Posts:
    75
    Hi there. as barrelmaker84 wrote at top, "and whenever I hit the play button to preview the animation", I was having the same "issue", it is, wherever I had positioned the character (any of them), after selecting one in hierarchy and click on play in preview animation window, the character began the animation at zero position. I thought it was a bug, but when entering in play mode (executing the game), everything worked ok, it is, the characters began the animation normally on their positions, and no one "translated" to zero position.