- Home /
Animation Event calling function
I am currently trying to call my function which uses raycast to damage my enemies. Calling Buttondown(); through an animation event mid swing through my sword so you cant spam fire1 and instantly kill the enemy. But now the animation event doesn't damage the enemy or debug log "hit". Any help would be apprecaited
The code worked before I decided to add the animation event and button down function so i'm not sure what the problem is, maybe i am calling the function incorrectly.
EDIT: Error message: 'Sword; AnimationEvent 'ButtonDown' has no receiver! Are you missing a component?
#pragma strict
var TheDamage : int = 50;
var Distance : float;
var MaxDistance : float = 1.5;
var TheSword : Transform;
//var TheSystem : Transform;
function Update ()
{
if (Input.GetButtonDown("Fire1"))
{
TheSword.animation.Play("AttackSword"); //plays animation where ButtonDown is called
}
}
function ButtonDown(){
//AttackDamage();
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
{
Distance = hit.distance;
if ( Distance < MaxDistance)
{
hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
Debug.Log("hit");
}
}
}
Can you deter$$anonymous$$e which part is broken? Is the ButtonDown method being called at all? I would put a quick debug at the top of that method if you have not already and confirm it is being called.
Also you might want to consider using a flag to control the rate of fire.
if (canAttack) {
canAttack = false;
TheSword.animation.Play("AttackSword");
ButtonDown(); // change this name to Attack() ?
}
Your animation could clear the canAttack flag or you could clear it in a coroutine with a timer. It just gives you more control.
I added a debug at the top of the ButtonDown function and it does not show that it is hitting also I tried adding a yield ForSeconds(1) function just after the damage and that does not work either.
Answer by Clint.Carpenter · Jun 26, 2015 at 05:19 PM
At least you now know that the animation is NOT calling your method. Try this:
function Update ()
{
if (Input.GetButtonDown("Fire1") && canAttack)
{
TheSword.animation.Play("AttackSword"); //plays animation where ButtonDown is called
canAttack = false;
ButtonDown();
StartCoroutine(WaitToAttack(1));
}
}
function ButtonDown(){
//AttackDamage();
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
{
Distance = hit.distance;
if ( Distance < MaxDistance)
{
hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
Debug.Log("hit");
}
}
}
function WaitToAttack (waitTime : float) {
// suspend execution for waitTime seconds
yield WaitForSeconds (waitTime);
canAttack = true;
}
Your answer
Follow this Question
Related Questions
Raycasting fail 1 Answer
GUI Counter Based on Animation or RaycastHit 0 Answers
If an specifc animation is playing dont let other animations play. 1 Answer
Mouse Over Wont Work on Bone Animated Meshes. 1 Answer
interacting with a raycast 1 Answer