Search Unity

Problem Moving Object in Code After Animation

Discussion in 'Animation' started by kayronjm, May 11, 2020.

  1. kayronjm

    kayronjm

    Joined:
    Jun 15, 2019
    Posts:
    34
    My game has weapon turrets whose barrels recoil when fired. The recoiling is controlled in code by defining the start and end local positions of the barrels and simply using Vector3.MoveTowards with certain recoil speeds. I've made sure the start and end local positions of the barrel are set properly on Start(). This works perfectly well. However, my weapon turrets can deploy and this is animated. One weapon turret in particular has its deployment animation move the barrel outwards. Again, this animation works perfectly.

    The only problem is that if I animate the barrel moving outwards, then the code for recoil doesn't seem to do anything. It seems as if when you animate an object, the animation controller takes sole control of it and code cannot change its position. Is this the case and if so, is there a setting I can toggle to fix this?

    It's odd because both the deployment animations and the recoil code work perfectly, and other weapon turrets where I haven't animated the barrel's position for deployment have absolutely no issue. It's just this one. Thank you!
     
  2. kayronjm

    kayronjm

    Joined:
    Jun 15, 2019
    Posts:
    34
    For reference, this is my animator's setup:



    The transitions are simply a trigger than the code calls.

    I can't seem to get it working. If the animation moves the barrel even by a tiny amount (much less than the barrel recoil), then it just won't recoil.
     
    Last edited: May 11, 2020
  3. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Your code needs to be in LateUpdate to replace whatever the Animator does every frame.
     
  4. kayronjm

    kayronjm

    Joined:
    Jun 15, 2019
    Posts:
    34
    Thank you. I will try this.

    I e-mailed you earlier regarding Animancer. I'm trying it out as I really like the idea of controlling everything via code. The issue I'm having is the AnimancerComponent class is not defined so on my code or your included examples, it isn't able to define a variable for it. For reference, I use the latest version of Unity 2019.3 and my project is in HDRP. I have tried it on the built-in pipeline too but no luck.
     
  5. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    That issue is not directly related to Animancer. It's usually caused by the Visual Studio Tools for Unity extension being out of date. You can update it via the Extensions menu in Visual Studio.

    There also seems to be an issue in Unity 2019.3.12 which prevents the VS Tools extension from working properly so if you are using that you could try downgrading to 2019.3.11.
     
    Last edited: May 12, 2020
  6. kayronjm

    kayronjm

    Joined:
    Jun 15, 2019
    Posts:
    34
    Thanks a lot for this. I had 2019.3.12 installed so I tried 2019.3.13 and 2019.3.11 and no luck on either. I updated Visual Studio Community 2019 completely and rebooted the PC and also no luck. Somehow though, throughout all this process, I went back to 2019.3.13 and noticed that now I can refer to the component via Animancer.AnimancerComponent, unless I set using Animancer at the top of the script, of course. Anyway, it appears to work and it's so much nicer than animation controllers, thank you. I will be buying the Pro version momentarily as I can see myself working more fluidly with Animancer, even if I don't yet exploit all its features.
     
    Kybernetik likes this.