Search Unity

Leaning script spazzing out

Discussion in 'Scripting' started by kiop2336, Aug 22, 2018.

  1. kiop2336

    kiop2336

    Joined:
    Feb 6, 2013
    Posts:
    10
    Ok so I attempted to make a simple leaning script with an animator, but every time I activate the lean left it has a spaz attack switching back and forth between the Idle2 animation and the LL (lean left) animation. Checking within unity I noticed that it's only activating the animation, not the bool, therefor making it switch like crazy. BUttttt, Lean right works just fine and I basically copy and pasted both pieces, so I am completely confused.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class leaning : MonoBehaviour {
    7.     public Animator anim;
    8.     public bool Leaning;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         Leaning = false;
    13.         anim = GetComponent<Animator>();
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.         if (Input.GetKey(KeyCode.Q))
    19.         {
    20.             anim.Play("LL");
    21.             Leaning = true;
    22.         }
    23.         else
    24.         {
    25.             Leaning = false;
    26.         }
    27.         if (Input.GetKey(KeyCode.E))
    28.         {
    29.             anim.Play("LR");
    30.             Leaning = true;
    31.         }
    32.         else
    33.         {
    34.             Leaning = false;
    35.         }
    36.  
    37.         if (!Leaning)
    38.         {
    39.             anim.Play("Idle2");
    40.         }
    41.     }
    42. }
    43.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You immediately set Leaning to false if E isn't down, even if Q is.
     
  3. kiop2336

    kiop2336

    Joined:
    Feb 6, 2013
    Posts:
    10
    Now that you mention it that does make sense. Any ideas on how to resolve that??