Search Unity

changing the animation repeatedly using if

Discussion in 'Scripting' started by FalconJ, Mar 25, 2015.

  1. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    I have this script :
    Code (CSharp):
    1. float movement = transform.position.x;
    2.  
    3. if (movement >= 0.25f) {
    4.                 animator.SetBool("right",true);
    5.             }
    6.             if(movement <= -0.25f){
    7.                 animator.SetBool ("left", true);
    8.             }
    I get the animation to change according to the if condition. But it only changed once (if I moved it >= 0.25f then the animation "right" will play, but when I moved it <=0.25f the animation still stays on "right" animation instead of changing into "left"). I want to make it always changed depending on my x-movement.
    Any ideas?
     
  2. Notter

    Notter

    Joined:
    Mar 8, 2015
    Posts:
    65
    I see you used 2 different Bool variabls
    so when you change "left" to true, you get this:

    right = true;
    left = true;

    Instead, you can make only one bool, let's say: "IsFacingRight".
    and the transition for "left" animation, is that it's false.
     
  3. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    well do you mean I have to do like this?
    Code (CSharp):
    1.     float movement = transform.position.x;
    2.    
    3.     if (movement >= 0.25f) {
    4.                     animator.SetBool("right",true);
    5.                    animator.SetBool("left",false);
    6.                 }
    7.                 if(movement <= -0.25f){
    8.                     animator.SetBool ("left", true);
    9.                     animator.SetBool("right",false);
    10.                 }
    11.  
    I tried that and the same problem still happening.
     
  4. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Did you try using a Blendtree to achieve this. Instead of getting lots of booleans true or false, you could just use a blendtree and make animator.SetFloat("movement"); and then configure your blendtree like run left (movement = -1), run right (movement = 1). The transition between those states is done by your mecanim blend tree. You can of course set the numbers close to zero, if you want a rapid change of your animation. :) Just an idea. Otherwise, of you want to go with your approach, the above code is right. Do not forget to make a transition back to your idle or run or whatever state with the condition of left or right being false :)
     
  5. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Oh, by the way. Did you debug.log your movement, if it really gets positive and negative? There could also be an error in that calculation which always returns a positive movement.
     
  6. Notter

    Notter

    Joined:
    Mar 8, 2015
    Posts:
    65
    No.. i said you can use a single bool, and you're still using two different bools.
    i meant something like this:

    Code (CSharp):
    1.     float movement = transform.position.x;
    2.    
    3.     if (movement >= 0.25f)
    4.                 {
    5.                     animator.SetBool("IsFacingRight",true);
    6.                 }
    7.     else
    8.                {
    9.                     animator.SetBool("IsFacingRight", false);
    10.                 }
    11.  
     
  7. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    I haven't tried using blend tree yet because I'm still not sure on how to use it.
    Anyway I already put the debug log and it responded well (if I moved it to right, the debug log will print the right version. vice versa). The problem still lies with only the first animation triggered that got played.
     
  8. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    Already tried this but still not what I want to achieve.This way the default state will already set at "facing right".
     
  9. Notter

    Notter

    Joined:
    Mar 8, 2015
    Posts:
    65
    I'm guessing you also have an idle state between left and right then?
    maybe try making it so you first set the animation to false, and after that setting the other to true.,

    also, probably something obvious, but you might have forgot to do it..
    do Right and Left have a transition between them?
    or, does "Any State" have transition to both?
     
    FalconJ likes this.
  10. vothka

    vothka

    Joined:
    Mar 27, 2015
    Posts:
    59
    first of all the question would be why your movement float would be your position.y
    of course this would also be possible but as you didn't state it explicitly i can only assume
    that a left - right orientation shoudln't be base on a world position

    i think what you actually want is
    float movement = Input.GetAxis("Horizontal");
     
  11. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    Well.. I forgot about the transition between left and right. Good tips, Notter. Thanks!
     
  12. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    I have another question regarding changing animation. Is it possible that the animation only play for a few seconds and then it changed back to the default state?
    For example, in my previous code, if I move the object to a certain x-position (let's say to the left), it will change the from the default animation (let's say "center") into "left" animation. The animation will stay like that as long as the x-position met the condition.
    What I really need is, when the object moved to the left, the animation will only play "left" for a few seconds before going back to "center" again.
     
  13. Notter

    Notter

    Joined:
    Mar 8, 2015
    Posts:
    65
    You're welcome.

    about the second question
    I think one way of doing it, is using a co-routine that waits for as long as you want, and then setting the animation to center.

    Another way, is giving the left/right animations a longer exit time (but that can cause problem if you also want instant transition)
     
  14. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    Can you give an example on how to apply coroutine to my code?
     
  15. Notter

    Notter

    Joined:
    Mar 8, 2015
    Posts:
    65
    Code (CSharp):
    1.  
    2. IEnumerator WaitForCenter()
    3. {
    4. // suspend execution for 5 seconds
    5. yield return new [URL='http://docs.unity3d.com/ScriptReference/WaitForSeconds.html']WaitForSeconds[/URL](5);
    6. Animator.SetBool();
    7. }
    8.  
    you call it with: StartCoroutine("WaitForCenter");

    i suggest you watch the unity instruction video on this subject