Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug two moves at the same time instead of two moves in a row

Discussion in 'Input System' started by DonPeppu, Aug 2, 2022.

  1. DonPeppu

    DonPeppu

    Joined:
    Aug 23, 2021
    Posts:
    22
    I'm creating a beat-'em-up 2D game. The player moves, runs, jumps, and there's no issue at all. Right now we are developing a sequence of three or four attacks with one key button and here's the issue: the attacks doesn't appear as in sequence, the player just gives all the attacks at the same time when the key button is pressed.
    Help! Thanks!

    this the code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class XPlayerControl : MonoBehaviour

    {
    public Rigidbody theRB;
    public float jumpForce;
    public float moveSpeed;
    private int groundMask;
    public Transform groundPoint;
    private float xAxis;
    private float yAxis;

    public GameObject colpo;
    private int colpoR =-1;
    private Vector3 colpoPos;
    public float colpoTime = 0.3f;
    public float colpoNext = 0.0f;
    private int colpoType = 0;


    private bool isAttackPressed;
    private float attackTipe;
    private int isTurbo = 1;
    private bool isGrounded;
    private Animator anim;
    private float attackDelay = 0.3f;
    public SpriteRenderer thSR;



    // Start is called before the first frame update
    void Start()
    {
    anim = GetComponent<Animator>();
    groundMask = 1 << LayerMask.NameToLayer("Ground");
    }

    // Update is called once per frame
    void Update()
    {
    if (Input.GetButton("Fire3"))
    {
    isTurbo = 2;
    }
    else
    {
    isTurbo = 1;
    }
    xAxis = -Input.GetAxis("Horizontal");
    yAxis = -Input.GetAxis("Vertical");
    theRB.velocity = new Vector3(xAxis * moveSpeed*isTurbo, theRB.velocity.y, yAxis * moveSpeed);
    //RaycastHit hit;
    if (Physics.Raycast(groundPoint.position, Vector3.down, 0.3f, groundMask))
    {
    isGrounded = true;
    anim.SetFloat("moveSpeed", theRB.velocity.magnitude);
    if (Input.GetButton("Fire2"))
    {
    theRB.velocity += new Vector3(0f, jumpForce, 0f);
    }
    }
    else
    {
    isGrounded = false;
    anim.SetFloat("moveSpeed", 100);
    }

    // COMBAT INPUT
    if (Input.GetButton("Fire1"))
    {
    if (Time.time > colpoNext)
    {
    colpoNext = Time.time + colpoTime;
    theRB.velocity = new Vector3(0, 0, 0);
    colpoPos = new Vector3((transform.position.x + 3 * colpoR), transform.position.y, transform.position.z);
    Instantiate(colpo, colpoPos, transform.rotation);
    attackTipe = 10;
    isAttackPressed = true;
    // StartCoroutine(colpoPause());
    if (Input.GetButton("Fire1") && attackTipe > 9)
    {
    colpoPos = new Vector3((transform.position.x + 8 * colpoR), transform.position.y, transform.position.z);
    Instantiate(colpo, colpoPos, transform.rotation);
    attackTipe = 20;
    }
    }
    }
    else
    {
    isAttackPressed = false;
    attackTipe = 0;
    colpoType = 0;

    }

    anim.SetBool("isAttack", isAttackPressed);
    anim.SetFloat("attack", attackTipe);


    if (!thSR.flipX && xAxis > 0)
    {
    thSR.flipX = true;
    colpoR = 1;
    }
    else if (thSR.flipX && xAxis < 0)
    {
    thSR.flipX = false;
    colpoR = -1;
    }



    }

    IEnumerator colpoPause()
    {
    yield return new WaitForSeconds(1);


    }


    }