melee attack
I'm trying to implement a melee attack in my fps game. I have it so that whenever the mouse is clicked, a swinging animation plays, and a raycast checks if there is a nearby enemy in front of the player. If there is, that enemy takes damage. The problem is my animation plays, but the enemy isn't taking any damage.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collision_Attack : MonoBehaviour
{
public float damage = 50.0F;
public GameObject Owner { get; private set; }
Animator anim;
AudioSource attack;
float Distance;
float MaxDistance = 2;
RaycastHit hit;
/*public void OnTriggerEnter(Collider other)
{
Debug.Log("Collision Occured");
Damageable damageable = other.GetComponent<Damageable>();
if (other.gameObject.tag == "Enemy")
if (damageable)
{
damageable.InflictDamage(damage, false, owner);
}
}*/
private void Start()
{
anim = gameObject.GetComponent<Animator>();
attack = gameObject.GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
anim.SetTrigger("Active");
attack.Play();
if (Physics.Raycast(transform.position, transform.forward, 100) && hit.transform.gameObject.tag == "Enemy")
{
Damageable damageable = hit.transform.GetComponent<Damageable>();
if (damageable)
{
damageable.InflictDamage(damage, false, Owner);
}
}
}
}
}
Comment