Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- /* berkinakkaya.wixsite.com/blog */
- public class PlayerAttack : MonoBehaviour {
- [SerializeField] float jumpDistance = 25f;
- [SerializeField] float knockBackPower = 10f;
- [SerializeField] LayerMask enemy;
- bool attacking;
- float lastHitTime;
- void Update()
- {
- if(Input.GetMouseButtonDown(0))
- StartCoroutine("Attack");
- }
- IEnumerator Attack()
- {
- PlayerMovement.PlayerRigidbody.AddForce(transform.right * jumpDistance, ForceMode2D.Impulse);
- attacking = true;
- yield return new WaitForSeconds(jumpDistance/10);
- attacking = false;
- }
- void Hit(GameObject enemy)
- {
- bool enoughtTimeElapsed = Time.time > lastHitTime + 0.1f;
- if(attacking && enoughtTimeElapsed)
- {
- Rigidbody2D hitRigidbody = enemy.GetComponent<Rigidbody2D>();
- Enemy enemyComponent = enemy.GetComponent<Enemy>();
- enemyComponent.ReceiveHit();
- Vector3 force = (transform.right * knockBackPower) + (transform.up * knockBackPower / 2);
- hitRigidbody.AddForce(force, ForceMode2D.Impulse);
- }
- lastHitTime = Time.time;
- }
- void OnCollisionStay2D(Collision2D col)
- {
- if(col.transform.CompareTag("Enemy")) Hit(col.gameObject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment