Disable And Enable Trigger

enter code here Hello Unity3D i have a question about isTrigger.How can i Enable the isTrigger when i do a certain animation and then disable it again.The reason i am asking this question is because the only way i can get the opponent to take damage is to have isTrigger enabled.But when its enabled the character takes damage but it takes damage even when i walk up to it.I don’t want that.I want it to take damage when i do an animation,such as punch or kick it.If anyone knows the answer.Can you please tell me how i can do this because for some reason i can’t find it?

function Update(){
if(DamageAnimation){
collider.isTrigger = true;
}
else
{
collider.isTrigger = false;
}
}
That’s how you’d do it. However, I’d recommend just calling a function when you want the enemy to take damage.

var Range = 5.0; // set this to the range the punch can affect
var myPlayer : GameObject;
var hand:Transform; // the hand or an empty gameObject near it
var AttackAnimName = “Punch”; // name of punch animation
var damage = 5; // the damage the punch can do
function Start () {

}


 function Punch () {
         var Hit : RaycastHit;
         var DirectionRay = transform.forward;
         Debug.DrawRay(transform.position, DirectionRay * Range, Color.blue);
         if(Physics.Raycast(hand.position, DirectionRay, Hit, Range))
         {
         myPlayer.animation.Play(AttackAnimName);
         if(Hit.transform.tag == "Enemy")      // remember to add Enemy tag to the enemy           
         {
// rename EnemyScript below to the damage recieving script on the enemy
          Hit.collider.GetComponent(EnemyScript).health = Hit.collider.GetComponent(EnemyScript).health - damage;  
         }
         }
 }
 
 function Update () {
 if(Input.GetButtonDown("Fire1")){
 Punch();
 }
 }

you could also use a raycast which can be used for any animation mostly.
for a kick etc
just set the range and animation name accordingly

Hello,

For melee attack the best solution is use Collider with Trigger, and to enable or disable the trigger collider you can do this editing the animation, so in X KeyFrame you can add a propertie to enable the Trigger and disable.

Cheers.

It worked for me…

.GetComponent<Image>().raycastTarget = boolValue

Like a button.interactable=boolValue;