please someone help me i ned to put sound on my character’s attack and respect the cooldown but i don’t know how can i do it, and my enemy’s attack.
this is my character atack script:
using UnityEngine;
using System.Collections;
public class PlayerAttack : MonoBehaviour {
public GameObject currentTarget;
public GameObject currentTarget2;
public float attackTimer;
public float coolDown;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 0.5f;
}
// Update is called once per frame
void Update ()
{
if(attackTimer > 0 )
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(Input.GetMouseButtonDown(0))
{
if(attackTimer == 0)
{
Attack();
attackTimer = coolDown;
}
}
}
private void Attack()
{
if (currentTarget == null) return;
{
//if(Attack.gameObject.tag == "enemy");
float distance = Vector3.Distance(currentTarget.transform.position, transform.position);
Vector3 dir = (currentTarget.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
if(distance < 4.5f) {
if(direction > 0) {
EnemyHealth eh = (EnemyHealth)currentTarget.GetComponent("EnemyHealth");
eh.AdjustCurrentHealth(-30);
}
}
}
}
public void OnTriggerEnter(Collider hit)
{
if (hit.tag == "EnemyTrigger")
{
if (currentTarget == null)
currentTarget = hit.transform.root.gameObject;
}
}
public void OnTriggerStay(Collider hit)
{
if (hit.tag == "EnemyTrigger")
{
if (currentTarget == null)
currentTarget = hit.transform.root.gameObject;
}
}
public void OnTriggerExit(Collider hit)
{
if (hit.tag == "EnemyTrigger")
{
if (currentTarget != null)
currentTarget = null;
}
}
}
and this is my enemy attack code:
using UnityEngine;
using System.Collections;
public class EnemyAttack : MonoBehaviour {
public GameObject target;
public float attackTimer;
public float coolDown;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 0.5f;
}
// Update is called once per frame
void Update () {
if(attackTimer > 0 )
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(attackTimer == 0) {
Attack();
attackTimer = coolDown;
}
}
private void Attack() {
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
Debug.Log(direction);
if(distance < 2.5f) {
if(direction > 0) {
PlayerHealth eh = (PlayerHealth)target.GetComponent("PlayerHealth");
eh.AdjustCurrentHealth(-10);
}
}
}
}