Question by
matthewcooper · May 17, 2020 at 08:56 AM ·
c#scripting problemmobileunityeditorcombat
Mobile attack button isn't doing damage to enemy
Whenever I tap my attack button it doesn't do any damage to the enemy, I'm really not sure what's wrong with it, I'm developing for mobile, I'll include the script below: Does anyone know how to fix this problem I'm having?
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 GetDamage() {
return weaponDamage;
}
public float GetRange(){
return weaponRange;
}
}
}
Comment