- Home /
Assist with CollisionDetection
Hello everybody! Could you please help with making the SendMessage function (BTW Is it right to call it a function?) work once per animation even if there were numerous collisions. Or how can I make the collisions to be undetectable for a certain amount of time?
Sorry if there are mistakes, English is a foreign language to me.
#pragma strict
var otherObj : GameObject;
var Damage : int = 50;
function OnCollisionEnter (hit : Collision)
{
if(otherObj.animation["Attack"].enabled)
{
if(hit.gameObject.tag == "enemy")
{
hit.gameObject.SendMessage("ApplyDamage", Damage);
}
}
}
Answer by Tehnique · Jul 11, 2014 at 06:48 PM
You can check if the animation is playing using
if (!animation.IsPlaying("Attack"))
{
...
}
Just ignore the collisions while Attack is playing.
It should work with enabled also, but in both cases you should use a var to check if you sent the message. Something like:
var otherObj : GameObject;
var Damage : int = 50;
var hasSentAttackMessage: bool = false;
function OnCollisionEnter (hit : Collision)
{
if(otherObj.animation["Attack"].enabled && !hasSentAttackMessage)
{
if(hit.gameObject.tag == "enemy")
{
hit.gameObject.SendMessage("ApplyDamage", Damage);
hasSentAttackMessage = true;
}
}
else if(!otherObj.animation["Attack"].enabled)
{
hasSentAttackMessage = false;
}
}
For some reason with this code the damage is not applied at all. I'm not sure if this is what I need because the problem is that when I attack(sword hit) and, for example, drag the mouse Unity detects many collisions and the enemy takes the damage few times.
Well, if you really want to disable collision for the animation duration, you can call
Physics.IgnoreCollision(player_collider, enemy_collider, true);
then make a method that enables the collision back (using false in the above line), and call this method using Invoke after x seconds (where x is animation duration). Ex, for 2 sec: Invoke("EnableCollision", 2);
Could you please type the whole code because I am new to all this and I don't understand many things you might find obvious. I would be very grateful!
Something like:
var otherObj : GameObject;
var Damage : int = 50;
var attackDuration : int = 2;
function OnCollisionEnter (hit : Collision)
{
if(otherObj.animation["Attack"].enabled)
{
if(hit.gameObject.tag == "enemy")
{
hit.gameObject.Send$$anonymous$$essage("ApplyDamage", Damage);
Physics.IgnoreCollision(otherObj.collider , transform.gameObject.collider, true);
Invoke("EnableCollision", attackDuration );
}
}
}
function EnableCollision()
{
Physics.IgnoreCollision(otherObj.collider , transform.gameObject.collider, false);
}
I'd still try to use the var that tells if you sent the message, as in my initial answer, it's less resource intensive.
Unfortunately, it still detects many collisions. Well, thanks, anyway, that was very informative. I guess I need to try something else, there must be easier ways than the one I chose.
Your answer
![](https://koobas.hobune.stream/wayback/20220613153822im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Pixel Perfection collision on 3d animated object 0 Answers
How to make my player play a vault animation when i collide into a vaultable object? 1 Answer
Im NOT Switching Scenes!! PLEASE HELP!! 1 Answer
Collisions on Models... 2 Answers
How to get the first person controller to collide with an animated object? 1 Answer