Search Unity

Mecanim - bug or am i doing wrong?

Discussion in 'Animation' started by ade76, Apr 9, 2014.

  1. ade76

    ade76

    Joined:
    Apr 9, 2014
    Posts:
    19
    Hi

    I'm very new to Unity (using 4.3) and have hit a snag with animation. The problem is I cannot tell if this is a bug in Unity (as it feels a bit like one) or I'm doing something wrong.


    My simple aim is to be able to control the speed of an animation state via code. So as an example I have a spinning cube sequence and I want to slow it down over time but if the player clicks it it speeds up.

    So I dug around the forums and discovered that with the animator controller you cannot specify speed for an individual state.

    However I found a workaround using blend trees which did work!!! I created a blend tree on my state and used the same animation clip but changed the threshold and speed. I set this upto a float and it worked!!!!

    However as soon as i started to add another transition to control another aspect the original code no longer functioned. No matter what I do now in the project as in delete the animation and controllers and do afresh it is still broken?

    Anyone any ideas on why this would occur?


    I have pasted my code below...bu tlike i say this was working great until I went in to add a new transition.

    Code (csharp):
    1. #pragma strict
    2.  
    3. private var cube:GameObject;
    4. private var cubeAni:Animator;
    5.  
    6.  
    7. public var cubeSpeed:float = 3;
    8. public var cubeMaxSpeed:float = 4;
    9. private var speedBoost:float = 0.5;
    10.  
    11. function Start () {
    12.  
    13. cube = GameObject.FindWithTag("cube");
    14. cubeAni = cube.GetComponent("Animator");
    15.  
    16.  
    17. }
    18.  
    19. function Update () {
    20.  
    21.    
    22.     cubeAni.SetFloat("Speed", cubeSpeed);
    23.    
    24.    
    25.     if (Input.GetMouseButtonDown(0))
    26.          {
    27.          
    28.            //Create physics2d raycast to detect location of click
    29.            var hitm : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    30.        
    31.          
    32.            if(hitm.collider.gameObject.tag == "cube")
    33.            {
    34.                cubeSpeed = cubeSpeed + speedBoost;
    35.                cubeAni.SetFloat("Speed", cubeSpeed);
    36.                
    37.                Debug.Log("clicked the cube" + cubeAni.speed);
    38.            }
    39.          }
    40.  
    41.  
    42. }

    thanks in advance

    ade
     
    Last edited: Apr 9, 2014
  2. ade76

    ade76

    Joined:
    Apr 9, 2014
    Posts:
    19
    Ok I think I have solved this:)

    It was how i was trying to access the Animator component so changed:

    cube = GameObject.FindWithTag("cube");

    cubeAni = cube.GetComponent("Animator")


    TO:


    cubeAni = GameObject.Find("cube").GetComponent(Animator);


    and it seems to work again