Usage of multiple tags or some other workaround?
I've a projectile that I want to work for two tags "Enemy" and "Player" but after a bit of Google search: it doesn't seem like there is a way to tag them both (unless I'm mistaken?) but in that case, then how would I go about changing this part of the script, so that the projectile works on both the enemies and the player, when they are getting shot at?
void OnCollisionEnter (Collision collisionTemp) {
if(collisionTemp.gameObject.tag == "Enemy") {
collisionTemp.gameObject.SendMessage("TakeDamage", damage, SendMessageOptions.DontRequireReceiver);
ContactPoint contact = collisionTemp.contacts[0];
Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
Vector3 pos = contact.point;
Instantiate (lasterHitFXPrefab, pos, rot);
Destroy (gameObject);
}
}
Answer by MakinStuffLookGood · Nov 17, 2015 at 07:12 PM
There are many different ways of doing this, but since your question is asking specifically about Tags, I'll tailor this answer towards the Tag system.
For starters, always use gameObject.CompareTag(string tag)
instead of getting the tag and doing string comparison. This avoids an internal string access and is a minor performance benefit, especially if you have a lot of tag comparisons.
In your projectile class, you could define an array of acceptable target tags, and then when you want to check if it's a valid hit, compare against that list:
string[] targetTags = new string[] { "Player", "Enemy" };
bool IsTargetTag(GameObject go) {
for (int i = 0; i < targetTags.Length; i++) {
if (go.CompareTag(targetTags[i]))
return true;
}
return false;
}
Then in your existing code where you do the string comparison, just slot in IsTargetTag()
et voilà!
Hey and thanks for helping :) But I guess I should have posted the whole code, that's my bad: https://gist.github.com/fiskefyren/0362897928cef2af1b59 I just thought I could add another var to the collisionTemp or something really simple like that.
I'm guessing that I should make the String into a private var in the class, but what should I do with the other stuff? Toss it into Start() {...}? I'm pretty new at this >_>
No need for the rest of your code, I don't plan on writing/editing your class for you ;)
Fields are implicitly private, so:
private string[] targetTags = new string[] { "Player", "Enemy" }; is no different from what I originally put. You could also just make this public if you want and then you'll have it visible in the Inspector. This lets you add/edit the target tags on individual prefabs or whatever. Also note the difference between string
, and string[]
, one is just a single string, the latter is an array of strings. Big difference.
As for the "other stuff"... well no you can't put it in Start. It's a function that returns a bool. You can put it wherever you want inside the LaserProjectile
class as long as it's not inside another function. Then call it from inside that class. I get the feeling you're somewhat new to program$$anonymous$$g in general, if that's true and you need further explanation just let me know.
I've only been program$$anonymous$$g for 3-4 weeks, so yeah, I hardly know what I'm doing sometimes xD however, I do understand the difference between a String "whatever" and a String[] 1D array (though I don't really know if a string array can have more than one dimension...? but I digress!).
But I'm a bit confused about the purpose of IsTargetTag as a whole...
I don't really understand what gameobject there is being referred to with go (seems like you're making a new var for some gameobject) and then you use it with CompareTag.
As for the targetTag... it doesn't really seem to change anything from its public value. I mean, my collision complains about it not being able to convert a string[] to a string... I thought the idea was that it could get both values (tags) in the OnCollisionEnter...
Okay so let me try and break it down for you!
bool IsTargetTag(GameObject go)
is a function that accepts a GameObject and returns a bool (either true or false). If you read the code for the function, you'll see that it uses the GameObject you supply to it (`go`) and loops over all the tags you defined. If any of the CompareTag calls pass (return true), the function itself returns true, otherwise it returns false.
That all make sense?
So now look at your existing code where you do this:
if(collisionTemp.gameObject.tag == "Enemy") { //do stuff }
Ins$$anonymous$$d of comparing the tag of the collision gameObject to the literal value "Enemy", you ins$$anonymous$$d call the IsTargetTag function, passing in collisionTemp.gameObject!
ie:
if(IsTargetTag(collisionTemp.gameObject)) { //do stuff }
So when IsTargetTag
returns true, the if statement passes and the contents of the curly braces will execute, when it's false, they won't.
The only Unity thing going on here really is the use of gameObject.CompareTag()
. Everything else is just sort of general C# stuff (functions, return values, loops, etc.). I would recommend you be focusing on learning how to program as much as you are learning how to make games, because the former skill is transferable and is greatly beneficial to the latter.
I see, I guess I'm just having a hard time understanding why that is needed. Or maybe rather how does it really define the difference between the two different strings inside the string[]? I'm guessing that true would either be "Enemy" or "player" and false would then be the other? Or else I don't really see how it (the script / Unity) can differentiate between the two values. And if so, then adding 3 strings to the string[] wouldn't really work?
I'm trying to learn how to program, but to say it in a nice way: my head doesn't work right (autism, dyslexia, tinnitus and other good stuff...). Anyway, I've a terrible hard time putting theories into practice, it's something that I often fight with in regard to my ongoing Bachelor. So even though I've read a fair share about program$$anonymous$$g and seen quite the amount of video tuts, I'm still having a hard time really understanding why I do what I do, or rather, understand how some of the knowledge that I already have and how to use it for the things I want to do... e.g. just because I got an apple doesn't mean that I've any clue as how to make an apple cake with it (maybe that was a bad example, I dunno). Regardless, I'm visual learner and there really isn't much that can be done about that, so the only way that seems to work for me is to just program stuff (like games) and from there try to learn how to make the stuff I want. I mean, I could just bypass the need for even making this threat by just making another copy of the same Projectile script and then just add a different tag to that one. But I wanted to learn, but since I couldn't figure it out on my own, I came here. Sadly, I don't really have any people in my life I can rely on for anything, so either I try here or get nowhere because I often can't seem to put theories into practice. You would probably be pretty confused if you looked at my game, it's a mess of many different tuts that I've mixed together in order to create what I currently have.
Sorry about the rant. But thanks a lot for your help! It does now what I wanted it to do :)
You can put however many tags you want in that array:
string[] targetTags = new string[] { "Player", "Enemy", "Friendly NPCs", "Cats", "Dogs", "The $$anonymous$$itchen Sink" };
It doesn't matter. The method I gave you is going to check all of them. If any of them match the Tag of the collided object it will send your TakeDamage method to them.
Sorry to hear you are having difficultly learning to program. I won't touch on that much because it's a personal thing and off-topic. If you're finding C#/Unity is too difficult, perhaps look into Game $$anonymous$$aker, where there is a visual scripting engine. It sounds like it may be better suited to your strengths.
Your answer
Follow this Question
Related Questions
Need Help With Collider Script. 2 Answers
Tag Editor is all grayed out 0 Answers
Tag an gameobject at runtime after a condition completed 1 Answer
Find closest object with a certain tag 2 Answers
Imported asset with multiple different categories / tags 0 Answers