Search Unity

How to stop the movement on X while attaking?

Discussion in '2D' started by Flauros1, Jul 2, 2020.

  1. Flauros1

    Flauros1

    Joined:
    Jul 2, 2020
    Posts:
    22
    Hi guys i'm a begginer and I'm making a fan-game if Wonder Boy (Sega) un UNITY 2D.

    I have this problem:
    I need that my player can't move while he is doing an attack. Do you need muy movement script Code to solve my problem?

    I Hope you can help me, thanks for read me and sorry for my bad english
     
  2. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    916
    Hello @Flauros1,
    Yes, do post your current version of your movement script. That will help us give you better advice. When you do post your code, make sure to use the Insert Code feature in the message toolbar, to make it more readable.
     
  3. Flauros1

    Flauros1

    Joined:
    Jul 2, 2020
    Posts:
    22
    Hey bro!
    Tomorrow i will try to post my Code! If you could help me vía Twitter @worldafire i can send you a picture of my Code.

    I have a little mental disorder since I had 5 years old so for me is a bit difficult understand this forum options :').. that's why I send you my twitter.

    Anyway i will try It tomorrow so don't worry if you don't have a Twitter account.

    Thanks you so much for answer me and have a good day :)

    (I don't know if this is the second time I post THIS mesage ) love you all guys!
     
  4. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    916
    Try your best in posting your code here, as more developers can benefit from the information. If the Insert Code feature is a bit tricky, just paste the code straight into the message box and I'll do my best to decipher it.

    Have a great day, and I look forward seeing your code!
     
  5. Flauros1

    Flauros1

    Joined:
    Jul 2, 2020
    Posts:
    22
    The trigger to attack is called "Ataque" and the animation of the attack is called "Atacar"
    Here is my code:

    Animator anim;

    Rigidbody2D rb;

    public float fuerzaSalto;

    public bool enSuelo;

    public Transform refPie;

    public float velX = 10f;


    CircleCollider2D attackCollider;




    // Start is called before the first frame update

    void Start()

    {

    anim = GetComponent<Animator>();

    rb = GetComponent<Rigidbody2D>();


    attackCollider = transform.GetChild(0).GetComponent<CircleCollider2D>();

    attackCollider.enabled = false;



    }


    // Update is called once per frame

    void Update()

    {

    float movX;

    movX = Input.GetAxis("Horizontal");

    anim.SetFloat("absMovX", Mathf.Abs(movX));

    rb.velocity = new Vector2(velX * movX, rb.velocity.y);



    enSuelo = Physics2D.OverlapCircle(refPie.position, 1f, 1 << 8); // cuando el pie está cerca del suelo

    anim.SetBool("enSuelo", enSuelo);

    if (Input.GetButtonDown("Jump") && enSuelo)



    {

    rb.AddForce(new Vector2(0, fuerzaSalto), ForceMode2D.Impulse);



    }


    AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo(0);

    bool Ataque = stateInfo.IsName("Atacar");

    //animacion de ataque


    if (Input.GetKeyDown("f") && !Ataque)

    {

    anim.SetTrigger("Ataque");

    }


    {



    }



    //girarse

    if (movX < 0) transform.localScale = new Vector3(-5, 5, 1);

    if (movX > 0) transform.localScale = new Vector3(5, 5, 1);


    //camara

    {

    Camera.main.transform.position = new Vector3(transform.position.x, 0, -20);

    }



    }
     
  6. Mooxe

    Mooxe

    Joined:
    Sep 18, 2013
    Posts:
    19
    Place this at the top of your update function

    Code (CSharp):
    1. AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo(0);
    2.  
    3. bool Ataque = stateInfo.IsName("Atacar");
    and then modify this code -
    Code (CSharp):
    1.  
    2. rb.velocity = new Vector2(velX * movX, rb.velocity.y);
    To this -
    Code (CSharp):
    1.  
    2. if(!Ataque)
    3. rb.velocity = new Vector2(velX * movX, rb.velocity.y);
    4. else
    5. rb.velocity = new Vector2(0, rb.velocity.y);
    6.  
    Hopefully this works.
     
    Ted_Wikman likes this.
  7. Flauros1

    Flauros1

    Joined:
    Jul 2, 2020
    Posts:
    22
    It works Bro thanks you so much!!!

    But now when I attack and jump I can't move my player in the air while he's attacking.
    Maybe using something like "ground" I could fix It?
     
  8. Flauros1

    Flauros1

    Joined:
    Jul 2, 2020
    Posts:
    22
    Yeah I fixt It writing &&enSuelo that means that my character is in the ground.

    THANKS YOU SO.MUCH GUYS
     
  9. Mooxe

    Mooxe

    Joined:
    Sep 18, 2013
    Posts:
    19
    Hey, glad it works,

    Code (CSharp):
    1.  
    2. if(!Ataque || player.transform.position.y > 1f)
    3. rb.velocity = new Vector2(velX * movX, rb.velocity.y);
    4. else if (Ataque && player.transform.position.y <= 1f)
    5. rb.velocity = new Vector2(0, rb.velocity.y);
    6.  
    The above code gives you an idea of what you need to do.

    so if the player's position is higher than a certain value of your choice (1f in this case) then it allows normal movement
    and if the player's position is lesser than that, then it won't allow x-direction movement.

    You'll have to link your Player gameobject to the "player" in this script.
     
  10. Mooxe

    Mooxe

    Joined:
    Sep 18, 2013
    Posts:
    19
    Oh didn't see this lol, ignore my last post, glad to hear it works!
     
  11. Flauros1

    Flauros1

    Joined:
    Jul 2, 2020
    Posts:
    22
    Anyway thanks you so much for answer me ^^