- Home /
Question by
YazanDeAce · Sep 16, 2014 at 09:14 AM ·
optimization
Melee combat cooldown with animations
Hello, I've this problem, I am using the script from the tutorials and adjusted it to fit with my model's attack action. But now whenever I click the attack button nothing even happens. the major problem was that the animation doesn't occur due to cooldown. but now nothing happens at all
using UnityEngine;
using System.Collections;
public class PlayerAttack : MonoBehaviour {
public GameObject target;
public float attackcooldown;
public float attacktimer;
// Use this for initialization
void Start () {
attackcooldown=2.0f;
attacktimer=0;
}
// Update is called once per frame
void Update() {
if (attacktimer > 0)
attacktimer -= Time.deltaTime;
if (attacktimer < 0)
attacktimer = 0;
if (Input.GetKey(KeyCode.F) && !animation.isPlaying)
{
if(attacktimer == 0)
{
animation.Play("attack 1");
AttackingEnemy();
attacktimer = attackcooldown;
}
}
if (Input.GetKey(KeyCode.X))
{
animation.Play("attack 2");
}
if (Input.GetKey(KeyCode.Z))
{
animation.Play("attack 3");
}
}
private void AttackingEnemy(){
float distance = Vector3.Distance (target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot (dir, transform.forward);
if (distance < 2.5) {
if (direction > 0) {
EnemyHealth troll = (EnemyHealth)target.GetComponent ("EnemyHealth");
troll.adjusting (-10);
}
}
}
}
Comment
Answer by Cherno · Sep 16, 2014 at 12:00 PM
Did you look at your attackTimer while playing? Try to put Debug.Logs throughout your code to see what parts of the code get reached and what not.
It's fixed for me somehow, but the issue now is the enemy's animation interrupting my player's one (even though damage is calculated normally)