- Home /
Why is my collider not sending damage?
OK I have my mouse trap set up just like everything else but it's not working?
static var enemyHit : boolean =false; // toggle for attack mode
static var damageAmount:int; // hold player damage amount
function OnTriggerEnter ( other : Collider ) { // trigger events for collider on foot
if ( other.tag == "enemy" ) // if collider equals enemy object
{
enemyHit = true; // enable attacking
damageAmount = other.GetComponent ( EnemyController ).damageAmount; // take damage to player health
yield WaitForSeconds ( 1 ); // wait a second before checking for another hit
}
else if ( other.tag == "robot" ) // if collider equals enemy object
{
enemyHit = true; // enable attacking
damageAmount = other.GetComponent ( AiPatrol ).damageAmount; // take damage to player health
yield WaitForSeconds ( 1 ); // wait a second before checking for another hit
}
else if ( other.tag == "mouseTrap" ) // if collider equals enemy object
{
enemyHit = true; // enable attacking
damageAmount = other.GetComponent ( MouseTrap ).damageAmount; // take damage to player health
yield WaitForSeconds ( 1 ); // wait a second before checking for another hit
print ("Player was hurt by a Mouse Trap");
}
}
function OnTriggerExit ( other : Collider ) { // check for collider exiting
if ( other.tag == "enemy" ) // if tag enemey exits
{
enemyHit = false; // disable enemy hit ability
}
else if ( other.tag == "robot" ) // if tag enemey exits
{
enemyHit = false; // disable enemy hit ability
}
else if ( other.tag == "mouseTrap" ) // if tag enemey exits
{
enemyHit = false; // disable enemy hit ability
}
}
I figured maybe it might help to see this also Mouse Trap Script
var mouseTrapSnap : AudioClip; // Assign Mouse Trap Snap Audio
var playerHit : boolean = false; // enable player hit
var damageAmount : int = 25;
function Start () {
animation.wrapMode = WrapMode.Once; // set play animation mode to once
animation [ "snap" ].wrapMode = WrapMode.Once; // set animation to play once
}
function OnTriggerEnter ( other : Collider ) { // if collision with player
//var controller : CharacterController = GetComponent(CharacterController);
if ( other.tag == "Player" )
{
//Play Mouse Trap Snap Sound
audio.PlayClipAtPoint ( mouseTrapSnap, transform.position );
yield WaitForSeconds (0.2);
//Play Mouse Trap Snap Animation
animation.Play ( "snap" );
}
else if ( other.tag == "push" )
{
//Play Mouse Trap Snap Sound
audio.PlayClipAtPoint ( mouseTrapSnap, transform.position );
yield WaitForSeconds (0.2);
//Play Mouse Trap Snap Animation
animation.Play ( "snap" );
//Destroys Character Controller);
//Destroy(controller);
//dissable Collider by Destroying it
Destroy(collider);
}
}
function OnTriggerExit ( other : Collider ) {
//var controller : CharacterController = GetComponent(CharacterController);
if ( other.tag == "Player" )
{
yield WaitForSeconds (0.2);
//Destroys Character Controller);
//Destroy(controller);
//dissable Collider by Destroying it
Destroy(collider);
}
}
is it entering the "if ( other.tag == "mouseTrap" )" at all? Are you sure the gameObject is actually tagged, maybe not tagged or spelled differently.
Now what is not working? That would help us give you a faster and efficient answer if you let us know what is wrong.
yes the mouse trap in tagged "mouseTrap" yes the box collider is of a fair size to cover the whole mouse trap. I know he is reacting to it because in the "Controller System" script I have another code that makes him jump back and make a sound when he collides with the box collider on the mouse trap. The other thing is that he sometimes takes damage but most times not?
Thanks fasase for cleaning up my mess ;)
is the message printing? or is the damage the only problem? also, another stupid question, you made sure isTrigger is checked (I assume yes). Also, do you mean to set player damage amount equal to the others, or rather subtract from it (like hit points) as in: damageAmount -= other.GetComponent ( AiPatrol ).damageAmount;
Answer by Seth-Bergman · May 01, 2012 at 01:52 AM
Wait, you are saying the trap animation plays.. OK, so I would put it all in one, like:
var playerHit : boolean = false; // enable player hit var damageAmount : int = 25;
function Start ()
{ animation.wrapMode = WrapMode.Once; // set play animation mode to once
animation [ "snap" ].wrapMode = WrapMode.Once; // set animation to play once
}
function OnTriggerEnter ( other : Collider ) { // if collision with player
if ( other.tag == "Player" ) { //Play Mouse Trap Snap Sound
audio.PlayClipAtPoint ( mouseTrapSnap, transform.position );
yield WaitForSeconds (0.2); //Play Mouse Trap Snap Animation
animation.Play ( "snap" );
var otherScript = other.GetComponent(PlayerScript); // access player from trap
otherScript.damageAmount += damageAmount;
otherScript.enemyHit = true;
}
else if ( other.tag == "push" ) { //Play Mouse Trap Snap Sound
audio.PlayClipAtPoint ( mouseTrapSnap, transform.position );
yield WaitForSeconds (0.2); //Play Mouse Trap Snap Animation
animation.Play ( "snap" );
} }
function OnTriggerExit ( other : Collider ) {
if ( other.tag == "Player" ) {
Destroy(collider); }
}
also, not having the character controller is, I'm sure, causing the problem. did you adjust the script for that, it's hard to tell by the example.. not that you would need a character controller for this, but the script was looking for one (though i think you commented that out).. either way, not sure why.. maybe it has something to do with the signal being sent to the rigidbody, rather than the collider. Script reference seems to suggest this.
Sweeeeetness!! Now that worked!! Thanks Seth, problem solved :)
cool glad to help. $$anonymous$$oving forward, you could just create a function on the player, then call that the same way like:
otherScript.DoWhatever();
glad I could help!
Thanks Seth, this will help as I've no doubt I'll be creating many more dangerous obstacles like the trap that are animated and cause damage to the player if collided with :)
Answer by bompi88 · Apr 30, 2012 at 06:32 PM
The mouseTrap prefab has a well sized collider, and it is tagged as "mouseTrap"? one part rhetorical question, one part not.
Answer by Seth-Bergman · May 01, 2012 at 12:45 AM
If the trigger is being entered by the trap, then you could always call the other object from that same function, as in :
var otherScript = other.GetComponent(PlayerScript);
otherScript.damageAmount += damageAmount;
The player enters a trigger collider attached to the Trap.
so does the trap snap? If so, add the lines above to the trap's trigger function, and it will handle both sides at once..
The mouse trap plays a snap animation when the player triggers the collider. Not sure what you mean by that last bit? The players damagage is handles by a "Controller Collider hurt" script, very short and simple script.
else if ( other.tag == "mouseTrap" ) // if collider equals enemy object { enemyHit = true; // enable attacking damageAmount = other.GetComponent ( $$anonymous$$ouseTrap ).damageAmount; // take damage to player health yield WaitForSeconds ( 1 );
And again, the exact issue is still unclear.. Is the trigger function getting entered at all, by either object? Sounds like what you're saying is the trap is working, but not the player. In which case you should just send a message to the other object from the one that works.
Well, I'll try and explain as best I can. I made some additional changes and now have it where my player can roll a ball over the trap and that will set the trap off and remove the box collider, so that is working great, even my player when he enters the trap removes the box collider on the trap which is good so I know that, that part works. But his damage is not being either registered or sent which I just don't get at all because you can see from the script that the trap is set up the same as the robot and the enemy, the only real difference is that the trap does not have or need a character controller, which I just can;t see that being the problem.