I am a beginner at Unity in terms of skill so please explain as if you were talking to a child if you can!
PROBLEM
I would like to change these names here:
I would like to rename them for two reasons:
so they are more intelligible
because I am using many different assets from the store and each has a different hierarchy with different names and I want to standardize the names so that I can use the below code to determine which part of the creature's body was shot so that it works for every creature
public void CreatureHit(string bodyPart, GunInfo usedWeapon, float intensity) // for guns { usedWeapon.PlayHit(creatureSounds); if (creatureInfo.healthPoints > 0) // to prevent dead creatures from being shot { if ("Torso" == bodyPart || "LeftUpperArm" == bodyPart // if the part that was hit was the arms or torso || "RightUpperArm" == bodyPart || "LeftLowerArm" == bodyPart // if the part that was hit was the arms or torso || "RightLowerArm" == bodyPart) { creatureInfo.healthPoints -= usedWeapon.damage * intensity; // deal standard dmg if (creatureInfo.healthPoints <= 0) creatureInfo.deathType = CreatureInfo.BODYSHOT; } else if ("Head" == bodyPart) // if the part that was hit was the head { creatureInfo.healthPoints -= usedWeapon.damage * 10 * intensity; // deal 10x dmg audioSource.PlayOneShot(creatureSounds.hitHead, 1); if (creatureInfo.healthPoints <= 0) creatureInfo.deathType = CreatureInfo.HEADSHOT; } else if ("RightUpperLeg" == bodyPart || "LeftUpperLeg" == bodyPart || "RightLowerLeg" == bodyPart || "LeftLowerLeg" == bodyPart) { creatureInfo.healthPoints -= usedWeapon.damage / 2 * intensity; // deal half dmg if (creatureInfo.healthPoints <= 0) creatureInfo.deathType = CreatureInfo.BODYSHOT; } } }
WHAT I TRIED
I renamed them in the hierarchy but then the animations stopped working. I found an old thread from the Unity forum asking if this was possible in 2015 and the OP was told that it wasn't. There were some later technical replies and I felt overwhelmed so I thought I should just create my own thread.
NOTE: there are multiple dozens of characters each with 10 animations so ideally I need a very efficient solution.
CodePudding user response:
Not a direct answer to your question (yet - might give it shot later).
In general you still can't unfortunately. The AnimationClip
s are based on strings storing the property paths of the serialized fields and properties you are animating.
If the paths change because you renamed the object or change the hierarchy in general the connection is lost and the animation breaks.
It would probably be possible to write an editor script which can do that but this will be very complex and for sure not "efficient".
Here is a little example of a complete other topic related to animation clips but you can already get an impression how absolutely non-trivial it will be to implement this in an editor script ;)
But an alternative to get your code work with the names as they are might be using tags instead.
As I see it your code is based on three different cases so you could simply have a tag for each like e.g. Head
, Arms
, Legs
and assign and check those accordingly (GameObject.CompareTag
) and not touch the names and animations at all.
CodePudding user response:
I have 2 plans.
Create an empty GameObject under the node you want to rename, and attach the collider compoent on it.
CATRigHub001Bone004 └ CATRigHub001Bone004Bone001 └ Rig <-------- Collider
Rename the bone in editor and create a script to automatically rename it to its original name while playing.
public class Rename : MonoBehaviour { public string boneName; [NonSerialized] public string partName; void Awake() { partName = name; name = boneName; } }