Advertisement
Guest User

Punch with duration and Combo

a guest
Jul 24th, 2018
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.87 KB | None | 0 0
  1. //Pasted here, since stack overflow says 3000 characters exceeded...
  2. //This should work for what you want :D
  3. {
  4.      public float playerSpeed, jumpForce;
  5.      public bool grounded;
  6.      public Transform groundedEnd;
  7.      
  8.      //Assign these via inspector btw
  9.      [Tooltip("How long a punch lasts, from start to end")]
  10.      public float punchDuration;
  11.      [Tooltip("How long a combo lasts, should always be bigger than punchDuration")]
  12.      public float comboDuration;
  13.      
  14.      private float moveX;
  15.      private int comboCount;
  16.      private float punchTimeElapsed;
  17.      
  18.      
  19.      private bool hasCombo;
  20.      private bool attacking;
  21.      private bool faceRight;
  22.  
  23.      Animator anim;
  24.      Rigidbody2D rigidbody;
  25.  
  26.     void Awake()
  27.     {
  28.         grounded = false;
  29.         attacking = false;
  30.         hasCombo = false;
  31.         comboCount = 0;
  32.         punchTimeElapsed = 0f;
  33.         comboTimeElapsed = 0f;
  34.     }
  35.  
  36.      void Start()
  37.      {
  38.          anim = GetComponent<Animator> ();
  39.          rigidbody = GetComponent<Rigidbody2D>();
  40.      }
  41.  
  42.      // Update is called once per frame
  43.      void Update ()
  44.      {
  45.          Raycasting ();
  46.          attackInput ();   
  47.          TimersUpdate();         
  48.      }
  49.  
  50.      void FixedUpdate()
  51.      {
  52.          PlayerMove ();
  53.          handleAttacks ();
  54.          //resetValues ();
  55.      }
  56.      
  57.      void TimersUpdate()
  58.      {
  59.          if (attacking)
  60.          {
  61.              punchTimeElapsed += Time.deltaTime;
  62.              if ( punchTimeElapsed > punchDuration )
  63.                  EndPunch();
  64.          }
  65.          
  66.          if (hasCombo)
  67.          {
  68.              comboTimeElapsed += Time.deltaTime;
  69.              if ( comboTimeElapsed > comboDuration)
  70.                  EndCombo();
  71.          }
  72.      }
  73.      
  74.      void EndPunch()
  75.      {
  76.         attacking = false;//Now he can move btw
  77.         punchTimeElapsed = 0f;
  78.      }
  79.    
  80.     void EndCombo()
  81.     {
  82.         hasCombo = false;
  83.         comboTimeElapsed = 0f;
  84.         comboCount = 0;
  85.     }
  86.  
  87.      //attacking
  88.      private void handleAttacks ()
  89.      {
  90.          if (attacking)
  91.              anim.SetTrigger ("punch");//I don't know about your animator, so idk what this is supposed to do :P
  92.      }
  93.  
  94.      //attack inputs
  95.      private void attackInput()
  96.      {
  97.          if(Input.GetKeyDown(KeyCode.Q))
  98.          {
  99.              if (attacking == false)//So it won't re-attack
  100.              {
  101.                  attacking = true;
  102.                  hasCombo = true;
  103.                  comboCount++;
  104.                  rigidbody.velocity = Vector2.zero;//Makes sure it won't move
  105.              }
  106.              
  107.          }
  108.      }
  109.  
  110.      //player movement function
  111.      void PlayerMove ()
  112.      {
  113.  
  114.         //So it won't move, since when player attacks, velocity becomes zero
  115.         if (attacking == true)
  116.             return;
  117.            
  118.          moveX = Input.GetAxis ("Horizontal");
  119.          //Note that GetComponent<>() on every frame, is really heavy on performance, so you cache it once, and no worries after :)
  120.          rigidbody.velocity = new Vector2(moveX * playerSpeed, rigidbody.velocity.y);
  121.  
  122.          //in animator, when speed is above 0.01, play running animation
  123.          anim.SetFloat ("speed", Mathf.Abs(Input.GetAxis("Horizontal")));
  124.  
  125.          //move right
  126.          if(moveX < 0.0f && faceRight == false)
  127.              flipPlayer ();
  128.  
  129.          //move left
  130.          if(moveX > 0.0f && faceRight == true)
  131.              flipPlayer ();
  132.  
  133.          //jump
  134.          if(Input.GetKeyDown (KeyCode.Space) && (grounded == true))
  135.              rigidbody.AddForce (Vector2.up * jumpForce);
  136.      }
  137.  
  138.      //check if player is grounded
  139.      void Raycasting()
  140.      {
  141.          Debug.DrawLine (this.transform.position, groundedEnd.position, Color.green);
  142.          grounded = Physics2D.Linecast (this.transform.position, groundedEnd.position, 1 << LayerMask.NameToLayer("Ground"));
  143.      }
  144.  
  145.      void flipPlayer()
  146.      {
  147.          faceRight = !faceRight;
  148.          Vector2 localScale = gameObject.transform.localScale;
  149.          localScale.x *= -1;
  150.          transform.localScale = localScale;
  151.      }
  152.  
  153.      private void resetValues()
  154.      {
  155.          attacking = false;
  156.      }
  157.  }
  158.  
  159. //Do note: If your character/player still moves while attacking, the animator is the issue 100%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement