- Home /
please help with my melee attack animation
so i have this attack script and i can't see how i would put a animation of my sword swinging in it as well so it actually looks like i'm attacking instead of imaginary damage dealing here is my script i'm using C# and i just really need some help soon please
public class PlayerAttack : MonoBehaviour {
public GameObject target;
public float attackTimer;
public float coolDown;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 2.0f;
}
// Update is called once per frame
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(Input.GetKeyDown(KeyCode.F)) {
Attack ();
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) {
EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
eh.AddJustCurrentHealth(-5);
}
}
}
}
Answer by fafase · Sep 22, 2012 at 07:05 AM
I would put it in Attack like this. I have not tried it so report if that fails.Note also that I have slightly altered some parts of your code:
public class PlayerAttack : MonoBehaviour {
public GameObject target;
public float attackTimer;
public float coolDown;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 2.0f;
}
// Update is called once per frame
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
else
attackTimer = 0;
if(Input.GetKeyDown(KeyCode.F)&& !animation.isPlaying) {
Attack ();
if(attackTimer == 0){
Attack();
attackTimer = coolDown;
}
}
}
private void Attack() {
animation.Play("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) {
EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
eh.AddJustCurrentHealth(-5);
}
}
}
}
Thanks for the help it made it so the animation plays but now its not doing any damage when i attack?
never$$anonymous$$d i just had to change a few things thanks for the help
Your answer
Follow this Question
Related Questions
Play attack animation on mousebutton 1 Answer
Animations Combo 0 Answers
Melee attacking, using force and health values 1 Answer
Not identifying the collider? 2 Answers
2 Animations At once 0 Answers