How to write an if statement that reach the execution time of a method?

Hi I have really great puzzle for you guys because I couldnt solve myself. As you know with StrikeCurrentTarget() method we connect our animator with our script and I am wondering how can I reach the StrikeCurrentTarget() method’s execution time from another script since I want to make a “Hurt” animation.

When attacker hit defender’s “hurt animaton will start” I want to do this exactly when my attacker asset hit the defender. You see in the below my attacker has a sword and when he hit- EXACTLY IN THAT MOMENT- there should be reaction from my defender. A hurt animation.

You can see when exactly I am using the strikeCurrentTarget method. In that time when we use strikeCurrentTarget there should be the reaction animation from defender’s script.

cscscs

Hi Ege,

The Animation Event that you have on the timeline, StrikeCurrentTarget, is wired to the Attacker.cs script and it calls this method;

public void StrikeCurrentTarget(float damage)
{
	if (currentTarget)
	{
		Health health = currentTarget.GetComponent<Health>();

		if (health)
		{
			health.DealDamage(damage);
		}
	}
}

Using this method as an example, you can see that you have the currentTarget variable there, so you should be able to use the GetComponent method to find it’s Animator component.

You could create a new parameter of type bool within the defender animator, perhaps “isHurt”.

image

With access to the Animator component in script, you can then call the SetBool method, passing in a name of the parameter.

public void StrikeCurrentTarget(float damage)
{
	if (currentTarget)
	{
		Health health = currentTarget.GetComponent<Health>();

		if (health)
		{
			Animator animator = currentTarget.GetComponent<Animator>();
			animator.SetBool("isHurt", true);

			health.DealDamage(damage);
		}
	}
}

(Note, you would probably want to refactor the above, this is just done as a quick example for you.)

You would then create a new state within your Animator and create the transition between the previous state(s) and the Hurt state;

image

You would use your parameter as a condition in order for the state to change so that it only performs that Hurt animation, set within the Hurt state’s motion property, when this bool is set.

image

Hope this is of some use :slight_smile:


See also;

I think you will also want to play with the Interruption Source settings if you want the hurt effect to cancel another animation.

2 Likes

Hi, I think yes this is the case that I couldnt solve. My hurt animation lenght and my attacker’s attacking animation lenght dont equal so when it hit first time its okay but then its phase shifting @Rob @Anthony_Juarez

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms