Guest User

Untitled

a guest
May 16th, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. namespace Combat {
  4. public class PlayerCombat : MonoBehaviour {
  5. public Animator animator;
  6. Transform target;
  7. [SerializeField] float weaponRange = 2f;
  8. [SerializeField] float weaponDamage = 5f;
  9. public object currentWeaponConfig;
  10. void Start() {
  11.  
  12. animator = GetComponent<Animator>();
  13.  
  14. }
  15. public void KnightHeroAttack() {
  16.  
  17. animator.SetTrigger("KnightHeroAttack");
  18.  
  19. }
  20. //Animation Event
  21. void OldHit() {
  22.  
  23. Health healthComponent = target.GetComponent<Health>();
  24. healthComponent.TakeDamage(weaponDamage);
  25.  
  26. }
  27. void Hit() {
  28.  
  29. var hits = Physics.SphereCastAll(transform.position, 2.0f, transform.forward); //Collect objects in front of player
  30. Health nearestEnemy = null;
  31. float distanceCheck = 10000;
  32. foreach(var hit in hits) {
  33.  
  34. if(hit.transform == transform) continue; //ignore self
  35. if(hit.distance > distanceCheck) continue;
  36. Health potentialEnemy = hit.transform.GetComponent<Health>();
  37. if(potentialEnemy) {
  38.  
  39. nearestEnemy=potentialEnemy;
  40. distanceCheck=hit.distance;
  41. }
  42. }
  43.  
  44. if(distanceCheck < GetRange()) return; //this should actually also account for no enemy found
  45. target = nearestEnemy.gameObject.transform;
  46. OldHit();
  47. }
  48. public float GetRange() {
  49.  
  50. return weaponRange;
  51.  
  52. }
  53.  
  54. }
  55.  
  56. }
Add Comment
Please, Sign In to add comment