- Home /
A limit to the number of tags?
My question is how many different tags should exist in a project scene. In an mmo setting would it be reasonable to tag all players and each of their parts with their own unique tag? for example P52134H (P for player, 52134 the character creation number, and H is for the head. P52134LL, for the left leg, etc. This seems to me like this is not what the tag system was intended for, but it does seem more efficient then attaching a script to every part for collision detection with other rigid bodies. (realizing this would end up with millions of tags, which seems terrible to me for w.e reason)
For example I swig a sword at another players leg, then I want to detect that I hit player X, and it was his leg. With the above system I could check the tag string of the collider hit, and parse that string and handle it accordingly. I cant really think of any other way besides attaching scripts to every body part that hold information about what player it belongs to and what part of the body it is. Maybe that's the better route?
Any insight is appreciated, thanks :)
edit: using the names of the objects themselves is not ideal to our design, but if you think that's the best case, I could prob change the design to allow that.
Like you said I don't think the tag system is needed for things like that.
You could just use the parent hierarchy to find the player, if there is a gameobject player-> with all the parts inside. You can add everything to that object. Then when u hit the leg, just access the player script on the parent object.
but that would require a script on all body parts... I guess that's not terrible if it just contains the info of the collider part (character id and collider section)
Why do you need a script on every body part? You need a collider, ok, but I haven't seen you say why you need a script
How do you know what you are colliding with w/o one? I swing my sword, it hits some part of an enemies body. I can get what my sword is colliding with by the name, a tag, or a script. I suppose it could search through the hierarchy looking for the parent object that contains a script with that info, but that is time consu$$anonymous$$g as it would have to search for a specific component at every level above it.
Since your design has the child components of a parent
if(collider.transform.root.tag == "Enemy") or collider.transform.parent == ... if you are worried that player wont be root
then you know your weapon hit an enemy and collider.transform.name is body part name (adjust to fit your hierarchy)
Answer by Teddy_p · May 07, 2014 at 02:45 AM
hmm actually I may have thought of something ill give it a go and see if it works (it sorta sound inefficient in my head though)
Ok so basically I grab the object I collided with through the script on the sword (lets for example say it was enemy #56's leg, but we don't know that yet), then I find the root of the object I hit through transform.root which contains a dictionary of all the colliders (like in the example above). I do a search in the dictionary for the object I collided with, then do damage through that script. The one problem with this is that if I ever parent the character object to something else, then the root of the player's leg would not be the root of the character, and hence wouldn't have the script I need. At that point idk, Id have to do some hierarchical search for it with GetComponentInChildren()... but then if there are two characters parented under the same object (like a boat or something) then we could run into issues and I would have to search through all of them. eventually making it pretty inefficient. Ill just have to test this stuff out and see what works I guess.
Answer by Clet_ · May 06, 2014 at 11:22 PM
Tags aren't meant to be used that way and locating the parts at runtime with their name wouldn't be really efficient, but you could do that in the start function and store it in a dictionary instead. It would look like :
using System;
using System.Collections.Generic;
using UnityEngine;
public enum BodyParts {
Head,
Torso,
LeftArm,
RightArm,
LeftLeg,
RightLeg
}
public class Humanoid : MonoBehaviour {
private Dictionary<BodyParts, GameObject> bodyParts = new Dictionary<BodyParts, GameObject>();
private void Start () {
foreach(var obj in Enum.GetValues(typeof(BodyParts))) {
BodyParts part = (BodyParts)obj;
bodyParts.Add (part, transform.Find(part.ToString()));
}
}
}
how do I return what was hit back to the sword if I hit the leg. The leg will not contain this script, and has no way of getting to it without searching for it up through the character model hierarchy (using a script of its own)
Personnaly, I don't think your sword should speak directly to the leg and vice-versa.
You should keep a reference to them in parent class, and let that class communicate to its children. That way, the parent class gets the body part that has been hit and then, call whatever function you need to call in your sword component
Your answer
Follow this Question
Related Questions
Help With Colliders 3 Answers
Collision Layer 2 Answers
Collision detection problem 1 Answer
handling collision with two objects of the same tag 1 Answer
handling collision with two objects of the same tag 1 Answer