Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Pasted here, since stack overflow says 3000 characters exceeded...
- //This should work for what you want :D
- {
- public float playerSpeed, jumpForce;
- public bool grounded;
- public Transform groundedEnd;
- //Assign these via inspector btw
- [Tooltip("How long a punch lasts, from start to end")]
- public float punchDuration;
- [Tooltip("How long a combo lasts, should always be bigger than punchDuration")]
- public float comboDuration;
- private float moveX;
- private int comboCount;
- private float punchTimeElapsed;
- private bool hasCombo;
- private bool attacking;
- private bool faceRight;
- Animator anim;
- Rigidbody2D rigidbody;
- void Awake()
- {
- grounded = false;
- attacking = false;
- hasCombo = false;
- comboCount = 0;
- punchTimeElapsed = 0f;
- comboTimeElapsed = 0f;
- }
- void Start()
- {
- anim = GetComponent<Animator> ();
- rigidbody = GetComponent<Rigidbody2D>();
- }
- // Update is called once per frame
- void Update ()
- {
- Raycasting ();
- attackInput ();
- TimersUpdate();
- }
- void FixedUpdate()
- {
- PlayerMove ();
- handleAttacks ();
- //resetValues ();
- }
- void TimersUpdate()
- {
- if (attacking)
- {
- punchTimeElapsed += Time.deltaTime;
- if ( punchTimeElapsed > punchDuration )
- EndPunch();
- }
- if (hasCombo)
- {
- comboTimeElapsed += Time.deltaTime;
- if ( comboTimeElapsed > comboDuration)
- EndCombo();
- }
- }
- void EndPunch()
- {
- attacking = false;//Now he can move btw
- punchTimeElapsed = 0f;
- }
- void EndCombo()
- {
- hasCombo = false;
- comboTimeElapsed = 0f;
- comboCount = 0;
- }
- //attacking
- private void handleAttacks ()
- {
- if (attacking)
- anim.SetTrigger ("punch");//I don't know about your animator, so idk what this is supposed to do :P
- }
- //attack inputs
- private void attackInput()
- {
- if(Input.GetKeyDown(KeyCode.Q))
- {
- if (attacking == false)//So it won't re-attack
- {
- attacking = true;
- hasCombo = true;
- comboCount++;
- rigidbody.velocity = Vector2.zero;//Makes sure it won't move
- }
- }
- }
- //player movement function
- void PlayerMove ()
- {
- //So it won't move, since when player attacks, velocity becomes zero
- if (attacking == true)
- return;
- moveX = Input.GetAxis ("Horizontal");
- //Note that GetComponent<>() on every frame, is really heavy on performance, so you cache it once, and no worries after :)
- rigidbody.velocity = new Vector2(moveX * playerSpeed, rigidbody.velocity.y);
- //in animator, when speed is above 0.01, play running animation
- anim.SetFloat ("speed", Mathf.Abs(Input.GetAxis("Horizontal")));
- //move right
- if(moveX < 0.0f && faceRight == false)
- flipPlayer ();
- //move left
- if(moveX > 0.0f && faceRight == true)
- flipPlayer ();
- //jump
- if(Input.GetKeyDown (KeyCode.Space) && (grounded == true))
- rigidbody.AddForce (Vector2.up * jumpForce);
- }
- //check if player is grounded
- void Raycasting()
- {
- Debug.DrawLine (this.transform.position, groundedEnd.position, Color.green);
- grounded = Physics2D.Linecast (this.transform.position, groundedEnd.position, 1 << LayerMask.NameToLayer("Ground"));
- }
- void flipPlayer()
- {
- faceRight = !faceRight;
- Vector2 localScale = gameObject.transform.localScale;
- localScale.x *= -1;
- transform.localScale = localScale;
- }
- private void resetValues()
- {
- attacking = false;
- }
- }
- //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