So what im trying to do is when i press left mouse button animation plays and in middle of animation call function attack with will take some hp of another object but idk why but function is not working… ;\
Meele script
#pragma strict
var TheDammage : int = 50;
var Distance : float;
var MaxDistance : float = 1.5;
var TheSystem : Transform;
function Start () {
}
function Update () {
if (Input.GetButtonDown("Fire1"))
{
//Attack animation - IF YOU ARE GETTING ERRORS BECAUSE YOU HAVENT MADE AN ANIMATION AND ATTACHED IT, DELETE THE FOLOWING LINE.
animation.Play("Swing");
}
}
function attack() {
var hit : RaycastHit;
if (Physics.Raycast(TheSystem.transform.position, TheSystem.transform.forward, hit, MaxDistance))
{
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
}
}
Enemy script
#pragma strict
var Health = 100;
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
if(Health <= 0)
{
Dead();
}
}
function Dead()
{
Destroy (gameObject);
}
It seems that you’re not calling the attack method in your code. Only specially named event method get called automatically by Unity. Any other methods have to be called explicitly.
Try changing your Update method to this:
function Update()
{
if (Input.GetButtonDown("Fire1"))
{
animation.Play("Swing");
attack();
}
}