public void AssignToLookAnimator(FLookAnimator lookAnim)
        {
            if (!generatedFollow)
            {
                generatedFollow = new GameObject(transform.gameObject.name + "-MomentLookTarget");
                generatedFollow.transform.SetParent(transform);
                generatedFollow.transform.localPosition = offset;
            }

            lookAnim.SetMomentLookTransform(generatedFollow.transform);
            //lookAnim.SetLookTarget(generatedFollow.transform);
        }
        private void OnTriggerEnter(Collider other)
        {
            if (other.tag == "Player")
            {
                FLookAnimator lookAnimator = other.gameObject.GetComponent <FLookAnimator>();

                if (lookAnimator)
                {
                    generatedFollow = lookAnimator.SetMomentLookTarget(transform, offset, timeOfLooking);
                }
            }
        }
        /// <summary>
        /// Trying to find other's transform's head position using different methods
        /// </summary>
        public Vector3 TryFindHeadPositionInTarget(Transform other)
        {
            // First, let's see if other object have look animator attached to it
            FLookAnimator look = other.GetComponent <FLookAnimator>();

            if (look)
            {
                if (look.LeadBone)
                {
                    return(look.GetLookStartMeasurePosition());
                }
            }

            // Let's see if other object is using humanoid animator
            Animator animator = other.GetComponentInChildren <Animator>();

            if (animator)
            {
                if (animator.isHuman)
                {
                    if (animator.GetBoneTransform(HumanBodyBones.LeftEye))
                    {
                        return(animator.GetBoneTransform(HumanBodyBones.LeftEye).position);
                    }
                    else
                    if (animator.GetBoneTransform(HumanBodyBones.Head))
                    {
                        return(animator.GetBoneTransform(HumanBodyBones.Head).position);
                    }
                }
            }

            // Trying to predict head position using other object's bounds
            Renderer rend = other.GetComponentInChildren <Renderer>();

            if (!rend)
            {
                if (other.childCount > 0)
                {
                    rend = other.GetChild(0).GetComponentInChildren <Renderer>();
                }
            }

            if (rend)
            {
                Vector3 boundsHead = other.position;
                boundsHead += other.TransformVector(Vector3.up * (rend.bounds.max.y * 0.9f));
                boundsHead += other.TransformVector(Vector3.forward * (rend.bounds.max.z * 0.75f));
                return(boundsHead);
            }

            return(other.position);
        }
        /// <summary>
        /// Updating component from custom inspector helper method
        /// </summary>
        private bool UpdateCustomInspector(FLookAnimator Get, bool allowInPlaymode = true)
        {
            if (!allowInPlaymode)
            {
                if (Application.isPlaying)
                {
                    return(false);
                }
            }
            else
            {
                Get.UpdateForCustomInspector();
            }

            return(true);
        }
        private void OnTriggerEnter(Collider other)
        {
            if (other.tag == "Player")
            {
                FLookAnimator lookAnimator = other.gameObject.GetComponent <FLookAnimator>();

                if (lookAnimator)
                {
                    AddToPriority(this);
                    RefreshArray();

                    if (priorityList.Count > 0)
                    {
                        if (priorityList[0] == this)
                        {
                            AssignToLookAnimator(lookAnimator);
                        }
                    }
                }
            }
        }
        private void OnTriggerExit(Collider other)
        {
            if (other.tag == "Player")
            {
                FLookAnimator lookAnimator = other.gameObject.GetComponent <FLookAnimator>();

                if (lookAnimator)
                {
                    if (generatedFollow)
                    {
                        Destroy(generatedFollow);
                    }
                    RemoveFromPriority(this);
                    RefreshArray();

                    if (priorityList.Count > 0)
                    {
                        priorityList[0].AssignToLookAnimator(lookAnimator);
                    }
                }
            }
        }
Exemplo n.º 7
0
 private void Start()
 {
     npcLookAnimator = GetComponent <FLookAnimator>();
 }
        public override void OnInspectorGUI()
        {
            Undo.RecordObject(target, "Look Animator Inspector");

            serializedObject.Update();

            FLookAnimator Get   = (FLookAnimator)target;
            string        title = drawDefaultInspector ? " Default Inspector" : " " + Get._editor_displayName;

            if (!drawNewInspector)
            {
                title = " Old GUI Version";
            }


            //EditorGUILayout.PropertyField(serializedObject.FindProperty("targetLookAngles"));
            //EditorGUILayout.PropertyField(serializedObject.FindProperty("animatedLookAngles"));

            HeaderBoxMain(title, ref Get.drawGizmos, ref drawDefaultInspector, _TexLookAnimIcon, Get, 27);


            #region Default Inspector
            if (drawDefaultInspector)
            {
                #region Exluding from view not needed properties

                List <string> excludedVars = new List <string>();

                if (Get.BackBonesCount < 1)
                {
                    excludedVars.Add("BackBonesTransforms");
                    excludedVars.Add("BackBonesFalloff");
                    excludedVars.Add("m_script");
                }

                #endregion

                GUILayout.Space(5f);

                if (Get.BackBonesCount < 0)
                {
                    Get.BackBonesCount = 0;
                }

                // Draw default inspector without not needed properties
                DrawPropertiesExcluding(serializedObject, excludedVars.ToArray());
            }
            else
            #endregion

            {
                if (drawNewInspector)
                {
                    GUILayout.Space(4f);
                    DrawNewGUI();
                }
                else
                {
                    DrawOldGUI();
                }
            }

            // Apply changed parameters variables
            serializedObject.ApplyModifiedProperties();
        }
        /// <summary>
        /// Searching through component's owner to find clavicle / shoulder and upperarm bones
        /// </summary>
        private void FindCompensationBones(FLookAnimator headLook)
        {
            // First let's check if it's humanoid character, then we can get head bone transform from it
            Transform root = headLook.transform;

            if (headLook.BaseTransform)
            {
                root = headLook.BaseTransform;
            }

            Animator animator = root.GetComponentInChildren <Animator>();

            List <Transform> compensationBones = new List <Transform>();

            Transform headBone = headLook.LeadBone;

            if (animator)
            {
                if (animator.isHuman)
                {
                    Transform b = animator.GetBoneTransform(HumanBodyBones.LeftShoulder);
                    if (b)
                    {
                        compensationBones.Add(b);
                    }

                    b = animator.GetBoneTransform(HumanBodyBones.RightShoulder);
                    if (b)
                    {
                        compensationBones.Add(b);
                    }

                    b = animator.GetBoneTransform(HumanBodyBones.LeftUpperArm);
                    if (b)
                    {
                        compensationBones.Add(b);
                    }

                    b = animator.GetBoneTransform(HumanBodyBones.RightUpperArm);
                    if (b)
                    {
                        compensationBones.Add(b);
                    }

                    if (!headBone)
                    {
                        animator.GetBoneTransform(HumanBodyBones.Head);
                    }
                }
                else
                {
                    if (animator)
                    {
                        foreach (Transform t in animator.transform.GetComponentsInChildren <Transform>())
                        {
                            if (t.name.ToLower().Contains("clav"))
                            {
                                if (!compensationBones.Contains(t))
                                {
                                    compensationBones.Add(t);
                                }
                            }
                            else
                            if (t.name.ToLower().Contains("shoulder"))
                            {
                                if (!compensationBones.Contains(t))
                                {
                                    compensationBones.Add(t);
                                }
                            }
                            else
                            if (t.name.ToLower().Contains("uppera"))
                            {
                                if (!compensationBones.Contains(t))
                                {
                                    compensationBones.Add(t);
                                }
                            }
                        }
                    }
                }
            }

            if (compensationBones.Count != 0)
            {
                for (int i = 0; i < compensationBones.Count; i++)
                {
                    // Checking if this bone is not already in compensation bones list
                    bool already = false;
                    for (int c = 0; c < headLook.CompensationBones.Count; c++)
                    {
                        if (compensationBones[i] == headLook.CompensationBones[c].Transform)
                        {
                            already = true;
                            break;
                        }
                    }

                    if (already)
                    {
                        continue;
                    }

                    // Fill nulls if available
                    bool filled = false;
                    for (int c = 0; c < headLook.CompensationBones.Count; c++)
                    {
                        if (headLook.CompensationBones[c].Transform == null)
                        {
                            headLook.CompensationBones[c] = new FLookAnimator.CompensationBone(compensationBones[i]);
                            filled = true;
                            serializedObject.Update();
                            serializedObject.ApplyModifiedProperties();
                            break;
                        }
                    }

                    if (!filled)
                    {
                        headLook.CompensationBones.Add(new FLookAnimator.CompensationBone(compensationBones[i]));
                    }
                }

                for (int c = headLook.CompensationBones.Count - 1; c >= 0; c--)
                {
                    if (headLook.CompensationBones[c].Transform == null)
                    {
                        headLook.CompensationBones.RemoveAt(c);
                    }
                }

                compensationBonesCount = headLook.CompensationBones.Count;
            }
        }
        /// <summary>
        /// Searching through component's owner to find head or neck bone
        /// </summary>
        private void FindHeadBone(FLookAnimator headLook)
        {
            // First let's check if it's humanoid character, then we can get head bone transform from it
            Transform root = headLook.transform;

            if (headLook.BaseTransform)
            {
                root = headLook.BaseTransform;
            }

            Animator  animator         = root.GetComponentInChildren <Animator>();
            Transform animatorHeadBone = null;

            if (animator)
            {
                if (animator.isHuman)
                {
                    animatorHeadBone = animator.GetBoneTransform(HumanBodyBones.Head);
                }
            }

            List <SkinnedMeshRenderer> sMeshs = new List <SkinnedMeshRenderer>();// = root.GetComponentInChildren<SkinnedMeshRenderer>();

            foreach (var tr in root.GetComponentsInChildren <Transform>())
            {
                if (tr == null)
                {
                    continue;
                }
                SkinnedMeshRenderer sMesh = tr.GetComponent <SkinnedMeshRenderer>();
                if (sMesh)
                {
                    sMeshs.Add(sMesh);
                }
            }

            Transform leadBone = null;
            Transform probablyWrongTransform = null;

            for (int s = 0; s < sMeshs.Count; s++)
            {
                Transform t;

                for (int i = 0; i < sMeshs[s].bones.Length; i++)
                {
                    t = sMeshs[s].bones[i];
                    if (t.name.ToLower().Contains("head"))
                    {
                        if (t.parent == root)
                        {
                            continue;                   // If it's just mesh object from first depths
                        }
                        leadBone = t;
                        break;
                    }
                }

                if (!leadBone)
                {
                    for (int i = 0; i < sMeshs[s].bones.Length; i++)
                    {
                        t = sMeshs[s].bones[i];
                        if (t.name.ToLower().Contains("neck"))
                        {
                            leadBone = t;
                            break;
                        }
                    }
                }
            }


            foreach (Transform t in root.GetComponentsInChildren <Transform>())
            {
                if (t.name.ToLower().Contains("head"))
                {
                    if (t.GetComponent <SkinnedMeshRenderer>())
                    {
                        if (t.parent == root)
                        {
                            continue;                   // If it's just mesh object from first depths
                        }
                        probablyWrongTransform = t;
                        continue;
                    }

                    leadBone = t;
                    break;
                }
            }

            if (!leadBone)
            {
                foreach (Transform t in root.GetComponentsInChildren <Transform>())
                {
                    if (t.name.ToLower().Contains("neck"))
                    {
                        leadBone = t;
                        break;
                    }
                }
            }

            if (leadBone == null && animatorHeadBone != null)
            {
                leadBone = animatorHeadBone;
            }
            else
            if (leadBone != null && animatorHeadBone != null)
            {
                if (animatorHeadBone.name.ToLower().Contains("head"))
                {
                    leadBone = animatorHeadBone;
                }
                else
                if (!leadBone.name.ToLower().Contains("head"))
                {
                    leadBone = animatorHeadBone;
                }
            }

            if (leadBone)
            {
                headLook.LeadBone = leadBone;
            }
            else
            {
                if (probablyWrongTransform)
                {
                    Get.LeadBone = probablyWrongTransform;
                    Debug.LogWarning("[LOOK ANIMATOR] Found " + probablyWrongTransform + " but it's probably wrong transform");
                }
                else
                {
                    Debug.LogWarning("[LOOK ANIMATOR] Couldn't find any fitting bone");
                }
            }
        }