- Home /
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?
Answer by Sausagesauce · Oct 21, 2014 at 03:19 PM
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.
Answer by unitygamer · Oct 21, 2014 at 04:32 PM
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
You Are A Saint!.I am definitely going to try this!
Answer by allcoder · Mar 13, 2017 at 10:02 AM
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.
Answer by JuanDLRR · Sep 18, 2017 at 09:15 PM
It worked for me...
.GetComponent<Image>().raycastTarget = boolValue
Like a button.interactable=boolValue;
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Destroy On Collision 4 Answers
Collision problems 0 Answers
Can't detect isgrounded? Help 1 Answer