/// <summary> /// Creates a mount point at run-time. This won't persist unless you /// serialize the data yourself during run-time. /// </summary> /// <param name="rName">Name of the mount point</param> /// <param name="rBoneName">Bone name to attach it to or empty string to attach to root</param> /// <param name="rIgnoreParentScale">Determines if we should ignore the parent object's scale value</param> /// <returns></returns> public MountPoint CreateMountPoint(string rName, string rBoneName, bool rIgnoreParentScale) { // Create the mount point MountPoint lPoint = new MountPoint(); lPoint.Owner = gameObject; lPoint.Name = rName; lPoint.BoneName = rBoneName; // Attach it to the right bone Transform lParentTransform = gameObject.transform; lPoint.Anchor = new GameObject(); lPoint.Anchor.name = "MP_" + lPoint.Anchor.GetInstanceID(); if (lPoint.BoneName.Length > 0) { Animator lAnimator = gameObject.GetComponent <Animator>(); if (lAnimator != null) { int lBoneIndex = MountPoints.GetHumanBodyBoneID(lPoint.BoneName); if (lBoneIndex >= 0 && lBoneIndex <= (int)HumanBodyBones.LastBone) { lParentTransform = lAnimator.GetBoneTransform((HumanBodyBones)lBoneIndex); } else { Transform lBoneTransform = MountPoints.FindBone(transform, lPoint.BoneName); if (lBoneTransform != null) { lParentTransform = lBoneTransform; } } } } lPoint.Anchor.transform.position = lParentTransform.position; lPoint.Anchor.transform.rotation = lParentTransform.rotation; lPoint.Anchor.transform.parent = lParentTransform; // Initialize by ignoring the scale lPoint.IgnoreParentScale = rIgnoreParentScale; // Add to the list of mount points Points.Add(lPoint); // Return the point return(lPoint); }
/// <summary> /// Anchors the mount point to the specified bone, essencially making /// this mount point a child of the bone. /// </summary> /// <param name="rBoneName">Bone name to anchor to or empty string to anchor to the root</param> public void AnchorTo(string rBoneName) { Transform lParentTransform = _Owner.transform; if (_Anchor == null) { _Anchor = new GameObject(); _Anchor.name = "MP_" + _Anchor.GetInstanceID(); } _BoneName = rBoneName; if (_BoneName.Length > 0) { Animator lAnimator = _Owner.GetComponent <Animator>(); if (lAnimator != null) { int lBoneIndex = MountPoints.GetHumanBodyBoneID(_BoneName); if (lBoneIndex >= 0 && lBoneIndex < (int)HumanBodyBones.LastBone) { lParentTransform = lAnimator.GetBoneTransform((HumanBodyBones)lBoneIndex); } else { Transform lBoneTransform = MountPoints.FindBone(_Owner.transform, _BoneName); if (lBoneTransform != null) { lParentTransform = lBoneTransform; } } } } // Parent the mount point to this new transform. We don't // change it's position or rotation since we may need an offset. _Anchor.transform.parent = lParentTransform; }