Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- namespace Combat {
- public class PlayerCombat : MonoBehaviour {
- public Animator animator;
- Transform target;
- [SerializeField] float weaponRange = 2f;
- [SerializeField] float weaponDamage = 5f;
- public object currentWeaponConfig;
- void Start() {
- animator = GetComponent<Animator>();
- }
- public void KnightHeroAttack() {
- animator.SetTrigger("KnightHeroAttack");
- }
- //Animation Event
- void OldHit() {
- Health healthComponent = target.GetComponent<Health>();
- healthComponent.TakeDamage(weaponDamage);
- }
- void Hit() {
- var hits = Physics.SphereCastAll(transform.position, 2.0f, transform.forward); //Collect objects in front of player
- Health nearestEnemy = null;
- float distanceCheck = 10000;
- foreach(var hit in hits) {
- if(hit.transform == transform) continue; //ignore self
- if(hit.distance > distanceCheck) continue;
- Health potentialEnemy = hit.transform.GetComponent<Health>();
- if(potentialEnemy) {
- nearestEnemy=potentialEnemy;
- distanceCheck=hit.distance;
- }
- }
- if(distanceCheck < GetRange()) return; //this should actually also account for no enemy found
- target = nearestEnemy.gameObject.transform;
- OldHit();
- }
- public float GetRange() {
- return weaponRange;
- }
- }
- }
Add Comment
Please, Sign In to add comment