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.
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()));
}
}
}
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.