How to stop the movement on X while attaking?

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

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.

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 :slight_smile:

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

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!

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();

rb = GetComponent();

attackCollider = transform.GetChild(0).GetComponent();

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);

}

}

Place this at the top of your update function

AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo(0);

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

and then modify this code -

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

To this -

if(!Ataque)
rb.velocity = new Vector2(velX * movX, rb.velocity.y);
else
rb.velocity = new Vector2(0, rb.velocity.y);

Hopefully this works.

1 Like

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?

Yeah I fixt It writing &&enSuelo that means that my character is in the ground.

THANKS YOU SO.MUCH GUYS

Hey, glad it works,

if(!Ataque || player.transform.position.y > 1f)
rb.velocity = new Vector2(velX * movX, rb.velocity.y);
else if (Ataque && player.transform.position.y <= 1f)
rb.velocity = new Vector2(0, rb.velocity.y);

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.

Oh didn’t see this lol, ignore my last post, glad to hear it works!

Anyway thanks you so much for answer me ^^