Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Script not working when animation is enabled

Discussion in 'Editor & General Support' started by keshk, Sep 16, 2014.

  1. keshk

    keshk

    Joined:
    Dec 15, 2013
    Posts:
    6
    I am testing to make a cube move with a script. Also have a simple Roll animation for the cube which is supposed to activate whenever I move the cube.

    But the cube doesn't move when the animation is 'checked' as active under the inspector tab. The cube only moves if I disable animation thus not even able to see if the animation works properly when I move the cube. The cube is imported from Blender to Unity. Please advice what I am doing wrong.

    The script is as follows. It is a very simple and small test file thus I have attached my Unity and Blender files too at Dropbox for reference if that helps. Thank you.

    Link to Unity Project
    https://www.dropbox.com/sh/cvpjf26i31o1ell/AABmLMqYV4tPiG7qruph2D4Ra?dl=0

    Link to Blender Model and Animation
    https://www.dropbox.com/s/deowh3yk5wpse1u/box.blend?dl=0

    Movement script for cube:

    Code (CSharp):
    1.     public float speed = 10.0F;
    2.     Animator anim;
    3.  
    4.     void Update()
    5.     {
    6.         float translation = Input.GetAxis("Vertical") * speed;
    7.         translation *= Time.deltaTime;
    8.         transform.Translate(0, 0, translation);
    9.  
    10.         Animating(translation);
    11.     }
    12.  
    13.     void Animating(float v)
    14.     {
    15.         bool roll = v != 0f;
    16.         anim.SetBool("Roll", roll);
    17.     }
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    First of all, you need to assign the animator component to the anim variable, otherwise it will most likely throw an exception which you can find in the console.
    Code (CSharp):
    1. void Start()
    2. {
    3.         anim = GetComponent<Animator>();
    4. }