- Home /
OnTriggerEnter, if weapon tag collides with player tag then do this
Hi,
I'm writing the fighting system for a game I'm working on. I have one problem though.
How do I use OnTriggerEnter with the tag "enemyweapon" to detect when the player gets hit by the enemy's weapon? I need: if enemyweapon tag collides with player tag then do this. I need to get both collisions but it seems the collisions only detect the game object they're attached to.
How can I go about this? My enemies attack differs so that is why I can't have the fighting systems on different scripts.
I'm trying this, but it's not working, this script is attached to the enemy: (this is only the portion need for the collisions)
function OnTriggerExit(other: Collider)
{
if (other.CompareTag("PlayerSword")&&!isTriggered)
{
isTriggered=true;
SwordSwing();
}
if (other.CompareTag("EnemyWeapon")&&!isTriggered)
{
if(other.player.tag == "Player")
{
isTriggered=true;
EnemySwordSwing();
}
}
}
For general understanding of collision events, I just made that small example, to clear things up.
Answer by Common_Horse · May 27, 2012 at 05:01 AM
In Java:
function OnTriggerEnter( other : Collider) {
if(other.gameObject.tag == "enemyweapon") {
//Do stuff here
}
}
In C#:
void OnTriggerEnter( Collider other) {
if(other.gameObject.tag == "enemyweapon") {
//Do stuff here
}
}
If you need an explanation of the code, just ask :)
Answer by GC1983 · May 27, 2012 at 02:17 AM
Yes the OnTriggerEnter/Exit only works for the collision object its attached to. Now if you want to use the same script for different objects, just separate them through booleans.
var enemyObject : boolean;
var playerObject : boolean;
OnTriggerEnter(collision : Collider)
{
if(enemyObject)
{
if(collision.gameObject.tag == "Player")
{
isTriggered = true;
EnemySwordSwing();
}
}
if(playerObject)
{
if(collision.gameObject.tag == "Player")
{
isTriggered = true;
SwordSwing()
}
}
}
Just put this same script on both the objects needed and mark the appropriate boolean for each one, so the trigger will know which to use. This should do what you need if its what you are needing. Let me know if you have questions.
Thanks for this but I'm not sure how it works logically. How can either thing detect the other objects if they're aren't assigned with a var : GameObject? They are just boolean checkboxes. Also I'm using other.Collision not an actual collision.
Can you explain further?
Answer by GC1983 · May 27, 2012 at 02:50 AM
Theyre assigned through the tag. the collision.gameObject.tag == "" is all you need. Just make sure the objects you want to be affected by this script has the proper tag names. The booleans you will manually check before game start to the assigned object. Check the playerObject bool for the player and the enemyObject for the enemies. This will make the if statement within the bool accessible.
Well the object that has the enemyweapon tag to it is a separate game object. The whole system is attached to the actual enemy.
Also it says it can't find EnemySwordSwing(); you said to add this to another script. If I can send this through script then I think I can get it to work. I'll just set a OnTriggerEnter on the player and have the EnemySwordSwing(); event happen when he gets hit.
If the above doesn't work then how can I send the enemies attack I've entered to the player after he gets hit? The enemies attack varies because I can set it or I would attach a OnTriggerEnter script to the player. Like if I enter 10 attack for one enemy and 30 to another, how can the script detect this? Can I use a get component?
I'm using an equation to subtract the damage taken to the player.
playersStats.playersHealth = playersStats.playersHealth - enemiesAttack + playersStats.playerDefense * .1;
Well, if the EnemySwordSwing(); is being called as you had, the function should be in the same script. Otherwise, you would GetComponent to find it and use it. In the end. I would recommend giving the enemy and the player their own scripts. You attach the OnTriggerEnter() script to the weapon that is making the contact with the object.
Yes, but how can I call the enemy's attack through my player if I set it differently to each enemy?
You shouldnt have to. You should have the script attached to them. In the end, you should create a script for the player and one for the enemies. Each should do their thing for response of whatever interaction (damage, chase, attack, whatever...). I dont recommend using one script for both of them. This way you can keep track of what script effects what.
Your answer
Follow this Question
Related Questions
OnTriggerEnter doesn't read a tag of a moving object 1 Answer
How to work with tags ? 1 Answer
Help With OnTriggerEnter (Collider) 1 Answer
Why isnt OnTriggerEnter Tags working. 1 Answer
AI Attack Not Working! W/Video 1 Answer