Search Unity

Dont move with my stuff when not playing animations??

Discussion in 'Animation' started by anIndividual7, Apr 26, 2015.

  1. anIndividual7

    anIndividual7

    Joined:
    Apr 26, 2015
    Posts:
    12
    Not sure why this dosen't come up all the time...I want to animate with script & mecanim but not at same time.

    Why is mecanim moving my object transforms to the first frame of one of my animations every frame when no animations are playing?

    The only way to make mecanim do nothing seems to be switching off the component? I'd like to keep it switched on & ready to play animation when I want it to but the default behaviour here is just infuriating.

    I do not have root motion ticked and there are not that many more settings to fiddle with - I hope Im missing something obvious, please help.
     
  2. medhue

    medhue

    Joined:
    Aug 24, 2014
    Posts:
    176
    Sorry, but I'm confused by your use of Mecanim. As I understand it, mecanim works using Animator Controllers. So you put your animations into the Animator Controller, and then set up your parameters in the Animator Controller. In the Animator Controller, you will notice that the first animation you drop in will be orange. This is your default animation. If you want this animation to have no movement at all, then you could easily create an animation in Unity, and drag it in first as the default.
     
  3. anIndividual7

    anIndividual7

    Joined:
    Apr 26, 2015
    Posts:
    12
    Hi medhue would be nice if mecamin behaved like that.

    I've tried it with an empty state, with a blank animation & whatever I seem to do when I drag in the animation that alters transforms my objects get their position set every frame to the first frame of that animation when nothing is supposed to be playing. This behaviour seems...wrong to me any idea what could cause that?
     
  4. medhue

    medhue

    Joined:
    Aug 24, 2014
    Posts:
    176
    Some images of your set up would go a long way to understanding your problem. And, if you want, we could skype and share screens, so I can see how you are trying to work it. I'm medhue1 on skype.
     
  5. anIndividual7

    anIndividual7

    Joined:
    Apr 26, 2015
    Posts:
    12
    Ok heres some pics of a simplified version of the issue, first in edit mode you can see my camera script working properly & adjusting the persepective cameras Z position to match the ortho cams size:-





    Now in play mode you can see mecanim altering the perspective cameras position when its playing an empty state, swtich off the animatior & normal service is resumed.



    This is happening while having the animation that does alter the perspective cam's position present in the state machine & not even playing it.

    edit: file attached
     

    Attached Files:

    Last edited: Apr 27, 2015
  6. medhue

    medhue

    Joined:
    Aug 24, 2014
    Posts:
    176
    Ok, I looked at it, but I didn't see what you said. What I saw on investigating, was that the empty has no animation assigned to it. Maybe that was just my scene tho.
     
  7. anIndividual7

    anIndividual7

    Joined:
    Apr 26, 2015
    Posts:
    12
    hello again, thats correct there is no animation attached to the empty state yet mecanim is setting the position of the perspective camera every frame.
     
  8. medhue

    medhue

    Joined:
    Aug 24, 2014
    Posts:
    176
    Well, that might be your whole problem. If it has no animation, then it will use the position it last had. Of course, I'm just speculating.
     
  9. anIndividual7

    anIndividual7

    Joined:
    Apr 26, 2015
    Posts:
    12
    Could you explain what you mean here? Why would it do anything unless it was told to do something?
     
  10. medhue

    medhue

    Joined:
    Aug 24, 2014
    Posts:
    176
    I think the point is, that if you want it to do something specific, then you should make an animation for it.
     
  11. anIndividual7

    anIndividual7

    Joined:
    Apr 26, 2015
    Posts:
    12
    So what do I do if I specificly want it to do nothing? - Can I confirm with you that it was setting the perspective cameras position every frame when in the empty state & is this normal behaviour for mecanim?
     
  12. medhue

    medhue

    Joined:
    Aug 24, 2014
    Posts:
    176
    I personally did not see a change in the position. What I would try, if I were you, is I would create an animation in Unity, drag it into the empty, and then select the camera, and go into the animation window. There, just hit record and set a keyframe. Then, you might want to select the animation in the project, and set it to looping. That should keep the camera where it is.
     
  13. anIndividual7

    anIndividual7

    Joined:
    Apr 26, 2015
    Posts:
    12
    medhue, im not sure you understand the issue. Did you change the ortho size on the camera with the animator component? If you look at the cameras in the scene view & fiddle with the ortho size, then do the same in play mode you will see the problem i'm having. Thanks for taking the time btw :) And it would be good if i could just comfirm the behaviour shown in the pictures i posted.
     
  14. medhue

    medhue

    Joined:
    Aug 24, 2014
    Posts:
    176
    Sorry, man! I just don't see the same thing, even when I adjust the size. The animator isn't affecting the 2 cameras at all. Wish I could help.
     
  15. anIndividual7

    anIndividual7

    Joined:
    Apr 26, 2015
    Posts:
    12
    Here is a video clearly showing what is happening. This is quite a large problem for my project.

     
  16. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You have other scripts attached to the game object. Did you make sure that those are not the cause for the issue?
     
  17. anIndividual7

    anIndividual7

    Joined:
    Apr 26, 2015
    Posts:
    12
    Hello dantus, as far as i can tell yes i'm sure. The video shows that switching off the animator stops the perspective cameras position getting set by mecamin and correctly updated by the script.
     
  18. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I am just asking because the scripts could trigger the animator to do something. Did you try to disable the scripts and keep the Animator active? Does that change anything?
    Another possibility you may try out is to change the size, but without selecting the actual game object. When you have a game object selected, it can have different behavior. Though, this would be a bug, but the information helps to isolate the issue.
     
  19. medhue

    medhue

    Joined:
    Aug 24, 2014
    Posts:
    176
    Yeah, I see now what you mean. The thing is, why not change the size in the animation, and trigger it will script?
     
  20. anIndividual7

    anIndividual7

    Joined:
    Apr 26, 2015
    Posts:
    12
    Its really not the scripts :)

    When the scene is in play mode I cannot even move the perspective camera by draging it in the scene view. Disable the animator & i can - is this normal?
     
  21. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Yes this is the expected behaviour, as soon as you have one clip animating a property the animator take control of this property and start to write value. The only way to override this value is to write this value after the animation step in a LateUpdate callback.

    In the following example "New state" is a state without any motion.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class CameraTest : MonoBehaviour {
    6.  
    7.     private Camera thisCamera;
    8.     private Animator thisAnimator;
    9.     private float value = 10;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         thisCamera = GetComponent<Camera>();
    14.         thisAnimator = GetComponent<Animator>();
    15.     }
    16.  
    17.     void LateUpdate()
    18.     {
    19.         AnimatorStateInfo stateInfo = thisAnimator.GetCurrentAnimatorStateInfo(0);
    20.         if (stateInfo.IsName("New State"))
    21.         {
    22.             // Blend from scripted value to animated value
    23.             if (thisAnimator.IsInTransition(0))
    24.             {
    25.                 // Read the value written by the animation system
    26.                 float animatedValue = thisCamera.fieldOfView;
    27.              
    28.                 // Transition start at 0.9 sec and have a duration of 0.1 sec
    29.                 float smoothTime = 1.0f - (stateInfo.normalizedTime - Mathf.Floor(stateInfo.normalizedTime));
    30.                 float velocity = 0.0f;
    31.                 float newValue = Mathf.SmoothDamp(value, animatedValue, ref velocity, smoothTime);
    32.  
    33.                 thisCamera.fieldOfView = newValue;
    34.             }
    35.             else
    36.             {
    37.                 thisCamera.fieldOfView = value;
    38.             }
    39.         }
    40.     }
    41.  
    42.     void OnGUI()
    43.     {
    44.         value = GUILayout.HorizontalSlider(value, 10, 60, GUILayout.Width(100));
    45.     }
    46. }
     
unityunity