So, I want to start this question off by saying I am by no means, amazing at coding, so please don’t be to harsh in criticizing my code! I’m 17 and pretty much just learning before I start college. Now, I have a 2-D side scroller game basically, and I downloaded some free, 3-D assets from the game store to see if I could incorporate 3-D graphics, with my 2-D game setup. I have succeeded for the most part, however, I am trying to have some of my collisions only be detected during an animation, for example, a melee attack animation. I have checked whether or not it can actually collide with the enemy, so I know that is at least right. I tried looking on the forums for many days to see whether or not I could find a solution, but it seemed to evade me. Anyway, here is some of my code and hopefully it isn’t terrible.

public class MeleeCombat : MonoBehaviour
{

		void OnTriggerEnter2D (Collider2D other)
		{
				if (other.gameObject.tag == "Enemy") {
						//if (Player.Attacked) {
						Debug.Log ("Hi");
						//}
				}
		}
		
		void OnTriggerExit2D (Collider2D other)
		{
				if (other.gameObject.tag == "Enemy") {
						//	if (!Player.Attacked) {
						Debug.Log ("Bye");
						//}
				}
		}

This is part of the script I attached to my game object to detect whether or not it is colliding with any tag that is under “Enemy”. I had to comment out the if(!Player.Attacked) and what not, as I couldn’t manage to get it to work right.

Now, I have a separate script that is the Player script, which has my Player animator, I am trying to have it tell my Melee script which is above, to only detect collisions during the Attack animation.

private void FireProjectile ()
		{
				if (FireRate != 0) {
						if (_canFireIn > 0)
								return;
						
						if (FireProjectileEffect != null) {
								var effect = (GameObject)Instantiate (FireProjectileEffect, ProjectileFireLocation.position, ProjectileFireLocation.rotation);
								effect.transform.parent = transform;
						}
						var direction = _IsFacingRight ? Vector2.right : -Vector2.right;
		
						var projectile = (Projectile)Instantiate (Projectile, ProjectileFireLocation.position, ProjectileFireLocation.rotation);
						projectile.Initialize (gameObject, direction, _controller.Velocity);
		
						_canFireIn = FireRate;
				
						AudioSource.PlayClipAtPoint (PlayerShootSound, transform.position);
						Animator.SetTrigger ("Fire");
				}
		}

		private void Melee ()
		{
				if (FireRate == 0) {
						Animator.SetTrigger ("Fire");
				}
		}

		private void Attacking ()
		{
				if (this.Animator.GetCurrentAnimatorStateInfo (1).IsName ("Attack")) {
						// Avoid any reload.
						Attacked = false;
				}
				while (true) {
						Debug.Log ("Is now true");
						Attacked = true;
				}
		}

I was reading in another post to use something among the lines of this.Animator.GetCurrentAnimatorStateInfo, I didn’t quite understand it though. I’m more than likely doing something wrong there. Anyway, if someone could please point me in the right direction, as how to have when my character is going through his melee animation, to detect collisions for the enemy tag, at only that point, using 2-D colliders!

Thanks!

If I understand your problem, you should do something like that:

private int _playerAttackStateHash = Animator.StringToHash("Base Layer.PlayerAttack"); //Replace "PlayerAttack" with the name of your PlayerAttack state in the animator. Don't forget to mention the layer!

//then if you want to check if you are or not in the PlayerAttack state (meaning that your PlayerAttack animation is being played)
AnimatorStateInfo info = _animator.GetCurrentAnimatorStateInfo(0);        
if(info.nameHash == _playerAttackStateHash )
{
//The PlayerAttack animation is being played
}

_animator is you Animator attached to your GameObject off course!
Hope this will help :wink: