Search Unity

Question Trouble Getting "Wall_Jump" Animation to trigger consistently. Please Help.

Discussion in '2D' started by wxrg66, May 14, 2022.

  1. wxrg66

    wxrg66

    Joined:
    Apr 3, 2021
    Posts:
    17
    So Below is the code for my character controller. Its extremely messy right now and im sorry for that. Im still newish to programming and plan to go back and clean/ rework most of this. i just want to get it working consistently first.
    My problem is when i switch from the "Wall_Slide" animation to the "Wall_Jump" animation.
    The wall jump animation triggers most of the time, however there seems to be a strange edge case i cant find where the player character will stay "stuck" in the "Wall_Slide" animation until they land or start wall Sliding again. Before i was having problems with the Player getting stuck on the "Falling" animation. I tried including cases for this in my if statements but im not sure whats going wrong. Any advice would be greatly appreciated.
    I included a marker indicating where the animations are being controlled and where they stop being controlled to make things a bit easier to read.
    Anytime you see the function ChangeState() i am changing the animation state


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5.  
    6.  
    7. public class Character_Controller_2D : MonoBehaviour
    8. {
    9.     [SerializeField]
    10.     float moveSpeed, intMoveSpeed, jumpForce, hurtCoolDown,
    11.     intHurtCd, wallSlideSpeed, wallJumpTime, xinput, dashTime, intAtckMltplr,
    12.     rayCastLength;
    13.  
    14.     [SerializeField]
    15.     Transform groundCheck,frontCheck;
    16.     [SerializeField]
    17.     LayerMask whatIsGround;
    18.     [SerializeField]
    19.     int health;
    20.  
    21.     public int dmg;
    22.     public float atckMltplr;
    23.     public bool atckCharge;
    24.  
    25.     Animator anim, mC_anim;
    26.     Rigidbody2D rb;
    27.     Camera m_cam;
    28.     [SerializeField]
    29.     GameObject hitBox;
    30.     Transform fireSpot;
    31.     [SerializeField]
    32.     string quiver;
    33.     [SerializeField]
    34.     LineRenderer line;
    35.     [SerializeField]
    36.     List<GameObject> fuelCells;
    37.  
    38.  
    39.     const float groundcheckRadius = 0.2f;
    40.     const float frontCheckRadius = 0.2f;
    41.  
    42.   bool jumping,grounded, canMove, moving, canFlip = true, facingRight = true, hurtCd, wallSliding,     isTouchingFront, wallJumping, duringWallJmp, dash, canJump, shooting;
    43.     string currentState;
    44.     static bool playExists;
    45.  
    46.     // Start is called before the first frame update
    47.  
    48.     void Awake(){
    49.    
    50.         if(!playExists){
    51.             playExists = true;
    52.             DontDestroyOnLoad(this.gameObject);
    53.         }else
    54.         {
    55.             Destroy(gameObject);
    56.         }
    57.     }
    58.     void Start()
    59.     {
    60.         m_cam = Camera.main;
    61.         mC_anim = m_cam.GetComponent<Animator>();
    62.         canMove = true;
    63.         rb = GetComponent<Rigidbody2D>();
    64.         anim = GetComponent<Animator>();
    65.         jumping = false;
    66.         fireSpot = GameObject.FindGameObjectWithTag("FireSpot").transform;
    67.         intHurtCd = hurtCoolDown;
    68.         intMoveSpeed = moveSpeed;
    69.         atckCharge = false;
    70.         line.enabled = false;
    71.         hitBox.SetActive(false);
    72.    
    73.    
    74.    
    75.     }
    76.  
    77.     // Update is called once per frame
    78.     void Update()
    79.     {
    80.  
    81.    
    82.  
    83.         if(mC_anim == null || m_cam == null){
    84.          m_cam = Camera.main;
    85.          mC_anim = m_cam.GetComponent<Animator>();
    86.         }
    87.  
    88.        GameObject[] cells = GameObject.FindGameObjectsWithTag("Fuel_Cells");
    89.        for (int i = 0; i < cells.Length; i++)
    90.        {
    91.            if(!fuelCells.Contains(cells[i])){
    92.                fuelCells.Add(cells[i]);
    93.            }
    94.        }
    95.  
    96.        if(fuelCells.Count > 6){
    97.            Destroy(fuelCells[0]);
    98.            fuelCells.Remove(cells[0]);
    99.        }
    100.  
    101.        
    102.            
    103.        if(fuelCells.Count > 0){
    104.            if(fuelCells[0] != null){
    105.              if(fuelCells[0].GetComponent<Fuel_Cell>().fuel <= 0.0f){
    106.                  fuelCells.Remove(fuelCells[0]);
    107.  
    108.                 }
    109.             }else
    110.             {
    111.                 fuelCells.Remove(fuelCells[0]);
    112.             }
    113.         }
    114.          
    115.  
    116.         xinput = Input.GetAxis("Horizontal");
    117.  
    118.         RaycastHit2D hitInfo = Physics2D.Raycast(fireSpot.position, fireSpot.right, rayCastLength);
    119.      
    120.        if(hitInfo){
    121.          line.SetPosition(0, new Vector3(fireSpot.position.x, fireSpot.position.y, 10f));
    122.          line.SetPosition(1, new Vector3(hitInfo.point.x, hitInfo.point.y, 10f));
    123.      
    124.         }
    125.  
    126.    
    127.         if(health <= 0){
    128.             Die();
    129.         }
    130.  
    131.         if(hurtCd){
    132.             HurtCoolDown();
    133.         }
    134.  
    135.         if(Input.GetKeyDown(KeyCode.Space)){
    136.             shooting = true;
    137.             canMove = false;
    138.             ChangeState("Shoot");
    139.        
    140.  
    141.  
    142.      
    143.         }
    144.         if(Input.GetKeyUp(KeyCode.Space)){
    145.        
    146.             atckCharge = false;
    147.             shooting = false;
    148.        
    149.        
    150.        
    151.        
    152.         }
    153.    
    154.        
    155.        
    156.    
    157.  
    158.  
    159.         if(atckCharge){
    160.          rb.velocity = new Vector2(0.0f, 0.0f);
    161.          canMove = false;
    162.          canFlip = false;
    163.          atckMltplr += 0.002f;
    164.          line.enabled = true;
    165.          line.endWidth += .001f;
    166.          line.startWidth = line.endWidth;
    167.          mC_anim.Play("Cam_Shake_1");
    168.          canJump = false;
    169.          if(fuelCells.Count > 0){
    170.              fuelCells[0].GetComponent<Fuel_Cell>().DrainFuel(.005f);
    171.          }
    172.  
    173.      
    174.  
    175.          
    176.          if(hitInfo.collider.gameObject.tag == "Enemy"){
    177.              hitInfo.collider.gameObject.GetComponent<Enemy_Health>().TakeDmg(dmg);
    178.             }
    179.      
    180.             if(atckMltplr >= 2){
    181.              atckMltplr = 2;
    182.              atckCharge = false;
    183.          
    184.          
    185.             }
    186.             if(line.endWidth >= 1f){
    187.                 line.endWidth = 1f;
    188.            
    189.            
    190.             }
    191.         } else
    192.         {
    193.             line.endWidth = 0.5f;
    194.             line.startWidth = 0.5f;
    195.             line.enabled = false;
    196.         }
    197.  
    198.         if(!atckCharge && !shooting && !wallJumping && !hurtCd){
    199.             canJump = true;
    200.             canMove = true;
    201.             atckMltplr = 1;
    202.             line.endWidth = 0.5f;
    203.             line.startWidth = 0.5f;
    204.             line.enabled = false;
    205.             shooting = false;
    206.        
    207.  
    208.        
    209.        
    210.  
    211.         }
    212.  
    213.  
    214.         if(Input.GetKeyDown(KeyCode.S) && !grounded){
    215.             rb.gravityScale = rb.gravityScale * 5;
    216.             hitBox.SetActive(true);
    217.  
    218.         }else
    219.         {
    220.             if(grounded || wallSliding){
    221.                 rb.gravityScale = 3;
    222.                 hitBox.SetActive(false);
    223.             }
    224.         }
    225.            
    226.        
    227. //BEGGINING OF ANIMATIONS.
    228.        
    229.  
    230.    
    231.        
    232.         if(!hurtCd && !isTouchingFront && !atckCharge && grounded && !shooting){
    233.             canMove = true;
    234.             canFlip = true;
    235.             canJump = true;
    236.        
    237.         }
    238.  
    239.         if(!moving && !jumping && grounded){
    240.             duringWallJmp = false;
    241.             if(!shooting){
    242.              ChangeState("Idle");
    243.             }
    244.  
    245.         }
    246.  
    247.         if(!grounded && !jumping && !dash && !wallSliding && !duringWallJmp){
    248.             ChangeState("Falling");
    249.         }
    250.  
    251.         if(Input.GetKeyDown(KeyCode.W) && grounded){
    252.             Jump();
    253.        
    254.        
    255.         }
    256.         if(!grounded && jumping && !dash){
    257.          ChangeState("Jump");
    258.         }
    259.         if(Input.GetKeyUp(KeyCode.W) && jumping){
    260.             rb.velocity = new Vector2(0.0f, -0.0f);
    261.             jumping = false;
    262.         }
    263.  
    264.         if(isTouchingFront && !grounded && Mathf.Abs(Input.GetAxis("Horizontal")) > 0.0f){
    265.             wallSliding = true;
    266.             jumping = false;
    267.             duringWallJmp = false;
    268.        
    269.         }else
    270.         {
    271.            if(Input.GetKeyDown(KeyCode.S) || !shooting && !atckCharge && !isTouchingFront && !wallJumping && !hurtCd || !shooting && !atckCharge && grounded && !hurtCd){
    272.              wallSliding = false;
    273.              canFlip = true;
    274.              canMove = true;
    275.            }
    276.  
    277.  
    278.         }
    279.  
    280.         if(wallSliding){
    281.             ChangeState("Wall_Slide");
    282.             rb.velocity = new Vector2(0.0f, Mathf.Clamp(-wallSlideSpeed,-wallSlideSpeed, float.MaxValue));
    283.             canMove = false;
    284.             canFlip = false;
    285.             duringWallJmp = false;
    286.         }
    287.        
    288.        
    289.  
    290.        
    291.  
    292.         if(Input.GetKeyDown(KeyCode.W) && wallSliding){
    293.             wallJumping = true;
    294.             duringWallJmp = true;
    295.             wallSliding = false;
    296.             canFlip = false;
    297.             Invoke("TurnWallJumpOff", wallJumpTime);
    298.        
    299.        
    300.         }
    301.  
    302.        
    303.  
    304.  
    305.         if(wallJumping){
    306.             wallSliding = false;
    307.             duringWallJmp = true;
    308.             ChangeState("Wall_Jump");
    309.        
    310.             if(facingRight){
    311.                 rb.velocity = new Vector2(-5,10);
    312.             }else
    313.             {
    314.                 rb.velocity = new Vector2(5,10);
    315.             }
    316.  
    317.        
    318.         }
    319.  
    320.         if(Input.GetKeyDown(KeyCode.LeftShift)){
    321.             dash = true;
    322.             ChangeState("Dash");
    323.             Invoke("TurnDashOff", dashTime);
    324.         }
    325.  
    326.         if(dash){
    327.             moveSpeed = intMoveSpeed * 2;
    328.         }
    329.    
    330.    
    331.     }
    332.  
    333.     void FixedUpdate(){
    334.    
    335.         if(Mathf.Abs(Input.GetAxis("Horizontal")) > 0.0f && !shooting){
    336.          Move();
    337.      
    338.         }else
    339.         {
    340.             moving = false;
    341.         }
    342.    
    343.         Debug.Log(fuelCells.Count);
    344.    
    345.  
    346.         JumpStuff();
    347.         WallCheck();
    348.  
    349.         if(fuelCells.Count > 0){
    350.             if(fuelCells[0] != null){
    351.              fuelCells[0].transform.position = Vector2.MoveTowards(fuelCells[0].transform.position, new Vector2(transform.position.x, transform.position.y + .5f ), moveSpeed * Time.deltaTime);
    352.             }
    353.  
    354.             if(fuelCells.Count > 1){
    355.                 for (int i = 0; i < fuelCells.Count; i++)
    356.                 {
    357.                     if(i == 1 || i == 3){
    358.                         if(i < 3 && fuelCells[i] != null){
    359.                          fuelCells[i].transform.position = Vector2.MoveTowards(fuelCells[i].transform.position, new Vector2(transform.position.x - 1f , transform.position.y - 1f), moveSpeed * Time.deltaTime);
    360.                         }else
    361.                         {
    362.                             if(fuelCells[i] != null){
    363.                              fuelCells[i].transform.position = Vector2.MoveTowards(fuelCells[i].transform.position, new Vector2(transform.position.x - 1f , transform.position.y - 2f), moveSpeed * Time.deltaTime);
    364.                             }
    365.  
    366.                         }
    367.  
    368.                     }
    369.  
    370.                     if(i == 2 || i == 4){
    371.                         if(i < 4 && fuelCells[i] != null){
    372.                          fuelCells[i].transform.position = Vector2.MoveTowards(fuelCells[i].transform.position, new Vector2(transform.position.x + 1f , transform.position.y - 1f), moveSpeed * Time.deltaTime);
    373.                         }else
    374.                         {
    375.                             if(fuelCells[i] != null){
    376.                              fuelCells[i].transform.position = Vector2.MoveTowards(fuelCells[i].transform.position, new Vector2(transform.position.x + 1f , transform.position.y - 2f), moveSpeed * Time.deltaTime);
    377.                             }
    378.  
    379.                         }
    380.  
    381.                     }
    382.                
    383.                 }
    384.             }
    385.  
    386.             if(fuelCells.Count > 5){
    387.                 fuelCells[5].transform.position = Vector2.MoveTowards(fuelCells[5].transform.position, new Vector2(transform.position.x, transform.position.y - 2.5f ), moveSpeed * Time.deltaTime);
    388.             }
    389.         }
    390.  
    391.  
    392.  
    393.    
    394.  
    395.  
    396.     }
    397.  
    398.  
    399.     void Move(){
    400.         Flip();
    401.         if(canMove){
    402.          rb.velocity = new Vector2(Input.GetAxis("Horizontal") * moveSpeed, rb.velocity.y);
    403.          moving = true;
    404.      
    405.      
    406.           if(grounded && !dash){
    407.               ChangeState("Run");
    408.           }
    409.         }
    410.  
    411.     }
    412.  
    413.     void Jump(){
    414.         if(canJump){
    415.          rb.AddForce(new Vector2(0.0f, jumpForce));
    416.          jumping = true;
    417.         }
    418.  
    419.    
    420.  
    421.    
    422.     }
    423.  
    424.     void JumpStuff(){
    425.  
    426.         grounded = false;  
    427.         Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheck.position, groundcheckRadius, whatIsGround);
    428.  
    429.         for (int i = 0; i < colliders.Length; i++)
    430.         {
    431.             if(colliders[i].gameObject != gameObject)
    432.             {
    433.                grounded = true;
    434.            
    435.            
    436.             }
    437.         }
    438.     }
    439.  
    440.     void WallCheck(){
    441.         isTouchingFront = false;  
    442.         Collider2D[] colliders = Physics2D.OverlapCircleAll(frontCheck.position, frontCheckRadius, whatIsGround);
    443.  
    444.         for (int i = 0; i < colliders.Length; i++)
    445.         {
    446.             if(colliders[i].gameObject != gameObject)
    447.             {
    448.                isTouchingFront = true;
    449.            
    450.            
    451.             }
    452.         }
    453.     }
    454.  
    455.     void TurnWallJumpOff(){
    456.         wallJumping = false;
    457.         canMove = true;
    458.         canFlip = true;
    459.     }
    460.  
    461.     void TurnDashOff(){
    462.         dash = false;
    463.         moveSpeed = intMoveSpeed;
    464.     }
    465.  
    466.     void ChangeState(string newState){
    467.         if(newState == currentState) return;
    468.  
    469.         anim.Play(newState);
    470.         currentState = newState;
    471.     }
    472. //END OF ANIMATIONS.
    473.     public void Hurt(int dmg){
    474.        health -= dmg;
    475.        hurtCd = true;
    476.        mC_anim.Play("Cam_Shake_1");
    477.     }
    478.  
    479.     public void Shoot(){
    480.         if(fuelCells.Count > 0){
    481.              if(fuelCells[0].GetComponent<Fuel_Cell>().fuel > 0){
    482.                  atckCharge = true;
    483.              }
    484.             }
    485.     }
    486.  
    487.  
    488.  
    489.  
    490.  
    491.  
    492.     void Die(){
    493.        //Debug.Log("DEAD");
    494.     }
    495.  
    496.     void Flip()
    497.     {
    498.        if(canFlip)
    499.        {
    500.          if(Input.GetAxis("Horizontal") < 0.0f && facingRight)
    501.          {
    502.             transform.Rotate(0.0f, 180f, 0.0f);
    503.             facingRight = false;
    504.        
    505.          }else
    506.          {
    507.            if(Input.GetAxis("Horizontal") > 0.0f && !facingRight)
    508.             {
    509.               transform.Rotate(0.0f, -180f, 0.0f);
    510.               facingRight = true;
    511.             }
    512.          }
    513.         }
    514.  
    515.  
    516.    
    517.  
    518.     }
    519.  
    520.     public void ChangeLayer(){
    521.         gameObject.layer = 10;
    522.     }
    523.  
    524.     public void ChangeLayerBack(){
    525.         gameObject.layer = 8;
    526.     }
    527.  
    528.     void HurtCoolDown(){
    529.         canMove = false;
    530.         hurtCoolDown -= 1f;
    531.    
    532.         if(hurtCoolDown <= 0.0f){
    533.             canMove = true;
    534.             hurtCoolDown = intHurtCd;
    535.             hurtCd = false;
    536.         }
    537.     }
    538.    
    539.    
    540. }
    541.  
     
    Last edited: May 15, 2022
  2. wxrg66

    wxrg66

    Joined:
    Apr 3, 2021
    Posts:
    17
    I wasnt checking if the player was walljumping within the check to see if they were wall sliding.