inverse animation after wall Jump

hi community thanks for the help, im making a 2d game where the player can get in the walls and go up and down(ninja gaiden nes style), the X axis movement is not allowed, it only does it when is on the ground, so when the character is on the wall and the player presses the “Jump” button the character jumps to the opposite direction. For example if he is facing right and jumps then he goes left and the animation flips to, so the problem is that when the character touches the ground the X axis movement is wrong with the animation, for example if i go right the collider and everything goes right indeed but the animation is “running” like im pressing the left button. what i want is that when the character touches the ground the X axis stays correctly with the animation. any idea?? thanks!!!

This is my code so far:

using UnityEngine;
using System.Collections;

public class fos_controller_script : MonoBehaviour {

    public float maxSpeed = 6;
    public float normalSpeed = 4;
    public float noSpeed = 0;

    bool facingRight = true;
   
    Animator anim;
    Rigidbody2D rBody2D;
    GameObject character;

    bool grounded = false;
    bool touchingWall = false;




    public Transform groundCheck;
    public Transform wallCheck;




    float groundRadius = 0.05f;
    float wallRadius = 0.05f;
    float edgeCheckUpRadius = 0.05f;



    public LayerMask whatIsGround;
    public LayerMask whatIsWall;
    public LayerMask whatIsEdgeUp;



    public bool jump;

    public bool superSpeed = false ;

    public bool isMovingUp = false;
    public bool isMovingDown = false;


   

    public float jumpForce = 450f;


    ///wallJump Facing Right

    public float negativeWallJumpForceX = -700f;


    ///wallJump Facing left

    public float WallJumpForceX =  700f;




   


    void Start () {
       
        anim = GetComponent<Animator>();
        rBody2D = gameObject.GetComponent<Rigidbody2D>();


    }
   
    void SetGravity(){

        rBody2D.gravityScale = 0;

    }

    void FixedUpdate () {


        grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);

        touchingWall = Physics2D.OverlapCircle(wallCheck.position, wallRadius, whatIsWall);



        anim.SetBool("Ground", grounded);

        anim.SetBool("touchingWall", touchingWall);



        if(grounded && !touchingWall){

            anim.SetBool ("wallJumpRight", false);
            anim.SetBool ("wallJumpLeft", false);

            //normalSpeed

                        anim.SetFloat ("vSpeed", GetComponent<Rigidbody2D>().velocity.y);

                        float move = Input.GetAxis ("Horizontal");


                        anim.SetFloat ("Speed", Mathf.Abs (move));
       
                        GetComponent<Rigidbody2D>().velocity = new Vector2 (move * normalSpeed, GetComponent<Rigidbody2D>().velocity.y);

                        superSpeed = false;

                        anim.SetBool ("superSpeed", superSpeed);

       

            ///// Facing right or left
           
           
            if(move > 0 && !facingRight){


                Flip ();

            }


           
            else if(move < 0 && facingRight){


                Flip ();
            }


   

        //superSpeed / Joystick Button 15
   
   
        if (Input.GetButton ("Fire1") && Input.GetButton ("Horizontal")) {


                        float moveFaster = Input.GetAxis ("Horizontal");
           
                        anim.SetFloat ("Speed", Mathf.Abs (moveFaster));

                        GetComponent<Rigidbody2D>().velocity = new Vector2 (moveFaster * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);

                        superSpeed = true;
                       
                        anim.SetBool("superSpeed", superSpeed);
                }


        if (superSpeed) {
               
                        if (Input.GetButton("Jump")) {
                               
                               
                                superSpeed = true;
                                anim.SetBool ("superSpeed", superSpeed);
           
           
            }

                        }

        }



        //// Walltouching Code
       
        if (touchingWall) {


            superSpeed =false;
           
            grounded =false;
           
            anim.SetBool ("superSpeed", false);
           
            anim.SetBool ("Ground", false);
           
           
           
            rBody2D.gravityScale = 0;
           
            GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, 0);
           
            anim.SetBool ("touchingWall", true);
           
           
           
            if (Input.GetAxis ("Vertical") > 0) {
               
               
                rBody2D.gravityScale = 0;
               
                transform.Translate (0, 2 * Time.deltaTime, 0);
               
                anim.SetBool ("isMovingUp", true);


               
               
            }else {
               
                anim.SetBool ("isMovingUp", false);
               
               
               
            }
           
            if (Input.GetAxis ("Vertical") < 0) {
               
               
                rBody2D.gravityScale = 0;
               
                transform.Translate (0, -2 * Time.deltaTime, 0);   
               
                anim.SetBool ("isMovingDown", true);
               
               
            } else {
               
                anim.SetBool ("isMovingDown", false);
               
               
               
            }


            if (Input.GetButton ("Jump") && facingRight ) {



                Vector3 theScale = transform.localScale;
               
                theScale.x *= -1;
               
                transform.localScale = theScale;



                GetComponent<Rigidbody2D>().AddForce (new Vector2 (negativeWallJumpForceX, 450));
               
                GetComponent<Rigidbody2D>().velocity = new Vector2 (0, 0);




                rBody2D.gravityScale = 1;



                anim.SetBool ("isMovingUp", false);   
               
                anim.SetBool ("isMovingDown", false);



                   
               
            }

            if (Input.GetButton ("Jump") && !facingRight ) {


                Vector3 theScale = transform.localScale;
               
                theScale.x *= -1;
               
                transform.localScale = theScale;



                GetComponent<Rigidbody2D>().AddForce (new Vector2 (WallJumpForceX, 450));
               
                GetComponent<Rigidbody2D>().velocity = new Vector2 (0, 0);



                rBody2D.gravityScale = 1;

           

                anim.SetBool ("isMovingUp", false);   
               
                anim.SetBool ("isMovingDown", false);   


           
            }
           
   
           
        }
           
           
           
        }






void Update()
{


        ////Jump Button
       
       
        if (grounded && Input.GetButton ("Jump") ) {
           
            GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, 0);

            GetComponent<Rigidbody2D>().AddForce (new Vector2 (0, jumpForce));




            anim.SetBool("Ground", false);

           
           
        }


        //SuperSpeed
       
        if (Input.GetButton ("Fire1") && Input.GetButton ("Horizontal")) {
           
           
            float moveFaster = Input.GetAxis ("Horizontal");
           
            anim.SetFloat ("Speed", Mathf.Abs (moveFaster));
           
            GetComponent<Rigidbody2D>().velocity = new Vector2 (moveFaster * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
           
            superSpeed = true;
           
            anim.SetBool("superSpeed", superSpeed);

        }



       
    }

   

    void Flip(){

        if (grounded) {


            facingRight = !facingRight;
       
            Vector3 theScale = transform.localScale;

            theScale.x *= -1;

            transform.localScale = theScale;

        }


    }




   




   

   
   
   
   
}

try delete “else” in line 133

I did but didn’t do anything, the animation still wrong with the x axis

maybe 12. bool facingRight = true; set to false ? :eyes:

thanks for the answer, but no, the only thing it does now is just be inverse since the beginning

ok, last try :smile: 242. and 274. add line facingRight = !facingRight;

not working, now the only thing it does is jump up

ok, then make: public bool facingRight = true; And look in inspector in what time all going wrong. Check the “grounded” too.

and here is something wrong. If you flip your charakter, then you should change boolean too. And if you said, that add line facingRight = !facingRight; “not working, now the only thing it does is jump up” then maybe you should change negative and positive force. Or try first jump, then in fly flip. And use fixedupdate only for physic change (dont mix boolean in update and fixed update).

1 Like

i tried that and actually it works!! but now the character is doing the jump up because it´s detecting that is not facing right so he is doing it backwards, so what i did is define a coroutine and after a 0.5f seconds change the bool value to false and it worked!!!

if (Input.GetButton ("Jump") && facingRight ) { 



Vector3theScale = transform.localScale;

theScale.x *= -1;

transform.localScale = theScale;



GetComponent<Rigidbody2D>().AddForce (newVector2 (negativeWallJumpForceX, 450));

GetComponent<Rigidbody2D>().velocity = newVector2 (0, 0);




rBody2D.gravityScale = 1;



anim.SetBool ("isMovingUp", false); 

anim.SetBool ("isMovingDown", false);



StartCoroutine("FlipAfterWallJump");


 }


           
            }

this is the IEnumerator:

IEnumerator FlipAfterWallJump(){

        yield return new WaitForSeconds(0.5f);

        facingRight = false;
       
       
    }

thaks for the help!!!