Guest User

Untitled

a guest
May 26th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 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 = 1000;
  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.  
  38. if(potentialEnemy && hit.distance < distanceCheck) {
  39.  
  40. nearestEnemy=potentialEnemy;
  41. distanceCheck=hit.distance;
  42.  
  43. }
  44.  
  45. }
  46.  
  47. if(distanceCheck > GetRange() || nearestEnemy == null) return; //this should actually also account for no enemy found
  48. target = nearestEnemy.gameObject.transform;
  49. OldHit();
  50. }
  51. public float GetRange(){
  52.  
  53. return weaponRange;
  54.  
  55. }
  56.  
  57. }
  58.  
  59. }
Add Comment
Please, Sign In to add comment