Exemple #1
0
        public virtual void OnLost(Collider other)
        {
            if (tagsToDetect.Contains(other.gameObject.tag) && other.GetComponentInParent <vLookTarget>() != null)
            {
                var _currentLookTarget = other.GetComponentInParent <vLookTarget>();

                if (targetsInArea.Contains(_currentLookTarget))
                {
                    targetsInArea.Remove(_currentLookTarget);


                    if (_currentLookTarget == lastLookTarget)
                    {
                        _currentLookTarget.ExitLook(this);
                    }
                }
                SortTargets();
                if (targetsInArea.Count > 0)
                {
                    currentLookTarget = targetsInArea[0];
                }
                else
                {
                    currentLookTarget = null;
                }
            }
        }
Exemple #2
0
        void SortTargets()
        {
            interation += Time.deltaTime;
            if (interation > updateTargetInteration)
            {
                interation -= updateTargetInteration;
                if (targetsInArea == null || targetsInArea.Count < 2)
                {
                    if (targetsInArea != null && targetsInArea.Count > 0)
                    {
                        currentLookTarget = targetsInArea[0];
                    }
                    return;
                }

                for (int i = targetsInArea.Count - 1; i >= 0; i--)
                {
                    if (targetsInArea[i] == null)
                    {
                        targetsInArea.RemoveAt(i);
                    }
                }
                targetsInArea.Sort(delegate(vLookTarget c1, vLookTarget c2)
                {
                    return(Vector3.Distance(this.transform.position, c1 != null ? c1.transform.position : Vector3.one *Mathf.Infinity).CompareTo
                               ((Vector3.Distance(this.transform.position, c2 != null ? c2.transform.position : Vector3.one * Mathf.Infinity))));
                });
                if (targetsInArea.Count > 0)
                {
                    currentLookTarget = targetsInArea[0];
                }
            }
        }
Exemple #3
0
    static LookPoints GetLookPoints(vLookTarget lookTarget)
    {
        LookPoints points        = new LookPoints();
        var        centerArea    = lookTarget.centerArea;
        var        sizeArea      = lookTarget.sizeArea;
        var        lookTransform = lookTarget.transform;

        points.frontTopLeft     = new Vector3(centerArea.x - sizeArea.x, centerArea.y + sizeArea.y, centerArea.z - sizeArea.z);
        points.frontTopRight    = new Vector3(centerArea.x + sizeArea.x, centerArea.y + sizeArea.y, centerArea.z - sizeArea.z);
        points.frontBottomLeft  = new Vector3(centerArea.x - sizeArea.x, centerArea.y - sizeArea.y, centerArea.z - sizeArea.z);
        points.frontBottomRight = new Vector3(centerArea.x + sizeArea.x, centerArea.y - sizeArea.y, centerArea.z - sizeArea.z);
        points.backTopLeft      = new Vector3(centerArea.x - sizeArea.x, centerArea.y + sizeArea.y, centerArea.z + sizeArea.z);
        points.backTopRight     = new Vector3(centerArea.x + sizeArea.x, centerArea.y + sizeArea.y, centerArea.z + sizeArea.z);
        points.backBottomLeft   = new Vector3(centerArea.x - sizeArea.x, centerArea.y - sizeArea.y, centerArea.z + sizeArea.z);
        points.backBottomRight  = new Vector3(centerArea.x + sizeArea.x, centerArea.y - sizeArea.y, centerArea.z + sizeArea.z);

        points.frontTopLeft     = lookTransform.TransformPoint(points.frontTopLeft);
        points.frontTopRight    = lookTransform.TransformPoint(points.frontTopRight);
        points.frontBottomLeft  = lookTransform.TransformPoint(points.frontBottomLeft);
        points.frontBottomRight = lookTransform.TransformPoint(points.frontBottomRight);
        points.backTopLeft      = lookTransform.TransformPoint(points.backTopLeft);
        points.backTopRight     = lookTransform.TransformPoint(points.backTopRight);
        points.backBottomLeft   = lookTransform.TransformPoint(points.backBottomLeft);
        points.backBottomRight  = lookTransform.TransformPoint(points.backBottomRight);
        return(points);
    }
Exemple #4
0
 public virtual void RemoveLookTarget(vLookTarget target)
 {
     if (targetsInArea.Contains(target))
     {
         targetsInArea.Remove(target);
     }
     if (currentLookTarget == target)
     {
         currentLookTarget = null;
     }
 }
Exemple #5
0
 /// <summary>
 /// Set vLookTarget
 /// </summary>
 /// <param name="target"></param>
 public virtual void SetLookTarget(vLookTarget target, bool priority = false)
 {
     if (!targetsInArea.Contains(target))
     {
         targetsInArea.Add(target);
     }
     if (priority)
     {
         currentLookTarget = target;
     }
 }
Exemple #6
0
 public void RemoveLookTarget(vLookTarget target)
 {
     if (targetsInArea.Contains(target))
     {
         targetsInArea.Remove(target);
     }
     if (lookTarget == target)
     {
         lookTarget = null;
     }
 }
Exemple #7
0
 public virtual void OnDetect(Collider other)
 {
     if (tagsToDetect.Contains(other.gameObject.tag) && other.GetComponent <vLookTarget>() != null)
     {
         currentLookTarget = other.GetComponent <vLookTarget>();
         var headTrack = other.GetComponentInParent <vHeadTrack>();
         if (!targetsInArea.Contains(currentLookTarget) && (headTrack == null || headTrack != this))
         {
             targetsInArea.Add(currentLookTarget);
             SortTargets();
             currentLookTarget = targetsInArea[0];
         }
     }
 }
    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        if (skin != null)
        {
            GUI.skin = skin;
        }
        vLookTarget lTarget = (vLookTarget)target;

        GUILayout.BeginVertical("Look Target", "window");

        GUILayout.Space(30);
        EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Script"));
        EditorGUILayout.HelpBox("This component works with the vHeadTrack. Create a Collider and check the Trigger option to limit the area range to detect with the vHeadTrack if this object can be look at. Make sure to add the tag in the tagsToDetect list", MessageType.Info);
        GUILayout.Space(10);

        lTarget.visibleCheckType = (vLookTarget.VisibleCheckType)EditorGUILayout.EnumPopup("Visible check type", lTarget.visibleCheckType);
        lTarget.lookPointTarget  = (Transform)EditorGUILayout.ObjectField("LookPointTarget", lTarget.lookPointTarget, typeof(Transform), true);
        lTarget.useLimitToDetect = EditorGUILayout.Toggle("Use Limit To Detect", lTarget.useLimitToDetect);
        if (lTarget.useLimitToDetect)
        {
            lTarget.minDistanceToDetect = EditorGUILayout.FloatField("Min Distance To Detect", lTarget.minDistanceToDetect);
        }
        EditorGUILayout.HelpBox("The LookPointTarget is actual position that your character will look at.", MessageType.Info);

        if (lTarget.visibleCheckType != vLookTarget.VisibleCheckType.None)
        {
            GUILayout.BeginVertical("box");
            GUILayout.Box("Area to check if is visible", GUILayout.ExpandWidth(true));
            lTarget.centerArea = EditorGUILayout.Vector3Field("Center Area", lTarget.centerArea);
            if (lTarget.visibleCheckType == vLookTarget.VisibleCheckType.BoxCast)
            {
                lTarget.sizeArea = EditorGUILayout.Vector3Field("Size Area", lTarget.sizeArea);
                EditorGUILayout.HelpBox("The box area is usage for multiple  raycast for box corners", MessageType.Info);
            }
            else
            {
                EditorGUILayout.HelpBox("The center area is usage for single raycast\n See the green sphere gizmo", MessageType.Info);
            }
            GUILayout.EndVertical();
        }
        lTarget.HideObject = EditorGUILayout.Toggle("Is Hide", lTarget.HideObject);

        GUILayout.EndVertical();
        serializedObject.ApplyModifiedProperties();
        if (GUI.changed)
        {
            EditorUtility.SetDirty(target);
        }
    }
Exemple #9
0
 public void OnLost(Collider other)
 {
     if (tagsToDetect.Contains(other.gameObject.tag) && other.GetComponentInParent <vLookTarget>() != null)
     {
         lookTarget = other.GetComponentInParent <vLookTarget>();
         if (targetsInArea.Contains(lookTarget))
         {
             targetsInArea.Remove(lookTarget);
         }
         SortTargets();
         if (targetsInArea.Count > 0)
         {
             lookTarget = targetsInArea[0];
         }
         else
         {
             lookTarget = null;
         }
     }
 }
Exemple #10
0
        Vector3 GetLookPoint()
        {
            var distanceToLoock = 100;

            if (lookConditions && !IgnoreHeadTrack())
            {
                var dir = transform.forward;
                if (temporaryLookTime <= 0)
                {
                    var lookPosition = headPoint + (transform.forward * distanceToLoock);
                    if (followCamera)
                    {
                        lookPosition = (cameraMain.transform.position + (cameraMain.transform.forward * distanceToLoock));
                    }

                    dir = lookPosition - headPoint;
                    if ((followCamera && !alwaysFollowCamera) || !followCamera)
                    {
                        if (currentLookTarget != null && (currentLookTarget.ignoreHeadTrackAngle || TargetIsOnRange(currentLookTarget.lookPoint - headPoint)) && currentLookTarget.IsVisible(headPoint, obstacleLayer))
                        {
                            dir = currentLookTarget.lookPoint - headPoint;
                            if (currentLookTarget != lastLookTarget)
                            {
                                currentLookTarget.EnterLook(this);
                                lastLookTarget = currentLookTarget;
                            }
                        }

                        else if (simpleTarget != null)
                        {
                            dir = simpleTarget.position - headPoint;
                            if (currentLookTarget && currentLookTarget == lastLookTarget)
                            {
                                currentLookTarget.ExitLook(this);
                                lastLookTarget = null;
                            }
                        }
                        else if (currentLookTarget && currentLookTarget == lastLookTarget)
                        {
                            currentLookTarget.ExitLook(this);
                            lastLookTarget = null;
                        }
                    }
                }
                else
                {
                    dir = temporaryLookPoint - headPoint;
                    temporaryLookTime -= Time.deltaTime;
                    if (currentLookTarget && currentLookTarget == lastLookTarget)
                    {
                        currentLookTarget.ExitLook(this);
                        lastLookTarget = null;
                    }
                }

                var angle = GetTargetAngle(dir);
                if (cancelTrackOutOfAngle && (lastLookTarget == null || !lastLookTarget.ignoreHeadTrackAngle))
                {
                    if (TargetIsOnRange(dir))
                    {
                        if (animator.GetBool("IsStrafing") && !IsAnimatorTag("Upperbody Pose"))
                        {
                            SmoothValues(strafeHeadWeight, strafeBodyWeight, angle.x, angle.y);
                        }
                        else if (animator.GetBool("IsStrafing") && IsAnimatorTag("Upperbody Pose"))
                        {
                            SmoothValues(aimingHeadWeight, aimingBodyWeight, angle.x, angle.y);
                        }
                        else
                        {
                            SmoothValues(freeHeadWeight, freeBodyWeight, angle.x, angle.y);
                        }
                    }
                    else
                    {
                        SmoothValues();
                    }
                }
                else
                {
                    if (animator.GetBool("IsStrafing") && !IsAnimatorTag("Upperbody Pose"))
                    {
                        SmoothValues(strafeHeadWeight, strafeBodyWeight, angle.x, angle.y);
                    }
                    else if (animator.GetBool("IsStrafing") && IsAnimatorTag("Upperbody Pose"))
                    {
                        SmoothValues(aimingHeadWeight, aimingBodyWeight, angle.x, angle.y);
                    }
                    else
                    {
                        SmoothValues(freeHeadWeight, freeBodyWeight, angle.x, angle.y);
                    }
                }
                if (targetsInArea.Count > 1)
                {
                    SortTargets();
                }
            }
            else
            {
                SmoothValues();
                if (targetsInArea.Count > 1)
                {
                    SortTargets();
                }
            }

            var rotA          = Quaternion.AngleAxis(yRotation, transform.up);
            var rotB          = Quaternion.AngleAxis(xRotation, transform.right);
            var finalRotation = (rotA * rotB);
            var lookDirection = finalRotation * transform.forward;

            return(headPoint + (lookDirection * distanceToLoock));
        }
Exemple #11
0
    /// <summary>
    /// Check if anny corner points of LookTarget area is visible from observer
    /// </summary>
    /// <param name="lookTarget">principal transform of lookTarget</param>
    /// <param name="from">observer point</param>
    /// <param name="debug">Draw lines </param>
    /// <returns></returns>
    public static bool IsVisible(this vLookTarget lookTarget, Vector3 from, bool debug = false)
    {
        if (lookTarget.HideObject)
        {
            return(false);
        }
        LookPoints points = GetLookPoints(lookTarget);

        if (lookTarget.visibleCheckType == vLookTarget.VisibleCheckType.None)
        {
            return(true);
        }
        else if (lookTarget.visibleCheckType == vLookTarget.VisibleCheckType.SingleCast)
        {
            if (CastPoint(from, lookTarget.transform.TransformPoint(lookTarget.centerArea), lookTarget.transform, debug))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        else if (lookTarget.visibleCheckType == vLookTarget.VisibleCheckType.BoxCast)
        {
            if (CastPoint(from, points.frontTopLeft, lookTarget.transform, debug))
            {
                return(true);
            }

            if (CastPoint(from, points.frontTopRight, lookTarget.transform, debug))
            {
                return(true);
            }

            if (CastPoint(from, points.frontBottomLeft, lookTarget.transform, debug))
            {
                return(true);
            }

            if (CastPoint(from, points.frontBottomRight, lookTarget.transform, debug))
            {
                return(true);
            }

            if (CastPoint(from, points.backTopLeft, lookTarget.transform, debug))
            {
                return(true);
            }

            if (CastPoint(from, points.backTopRight, lookTarget.transform, debug))
            {
                return(true);
            }

            if (CastPoint(from, points.backBottomLeft, lookTarget.transform, debug))
            {
                return(true);
            }

            if (CastPoint(from, points.backBottomRight, lookTarget.transform, debug))
            {
                return(true);
            }
        }

        return(false);
    }
Exemple #12
0
    /// <summary>
    /// Check if anny corner points of LookTarget area is visible from observer
    /// </summary>
    /// <param name="lookTarget">principal transform of lookTarget</param>
    /// <param name="from">observer point</param>
    /// <param name="layerMask">Layer to check</param>
    /// <param name="debug">Draw lines </param>
    /// <returns></returns>
    public static bool IsVisible(this vLookTarget lookTarget, Vector3 from, LayerMask layerMask, bool debug = false)
    {
        if (lookTarget.HideObject)
        {
            return(false);
        }

        if (lookTarget.visibleCheckType == vLookTarget.VisibleCheckType.None)
        {
            if (lookTarget.useLimitToDetect && Vector3.Distance(from, lookTarget.transform.position) > lookTarget.minDistanceToDetect)
            {
                return(false);
            }

            return(true);
        }
        else if (lookTarget.visibleCheckType == vLookTarget.VisibleCheckType.SingleCast)
        {
            if (lookTarget.useLimitToDetect && Vector3.Distance(from, lookTarget.centerArea) > lookTarget.minDistanceToDetect)
            {
                return(false);
            }
            if (CastPoint(from, lookTarget.transform.TransformPoint(lookTarget.centerArea), lookTarget.transform, layerMask, debug))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        else if (lookTarget.visibleCheckType == vLookTarget.VisibleCheckType.BoxCast)
        {
            if (lookTarget.useLimitToDetect && Vector3.Distance(from, lookTarget.transform.position) > lookTarget.minDistanceToDetect)
            {
                return(false);
            }
            LookPoints points = GetLookPoints(lookTarget);

            if (CastPoint(from, points.frontTopLeft, lookTarget.transform, layerMask, debug))
            {
                return(true);
            }

            if (CastPoint(from, points.frontTopRight, lookTarget.transform, layerMask, debug))
            {
                return(true);
            }

            if (CastPoint(from, points.frontBottomLeft, lookTarget.transform, layerMask, debug))
            {
                return(true);
            }

            if (CastPoint(from, points.frontBottomRight, lookTarget.transform, layerMask, debug))
            {
                return(true);
            }

            if (CastPoint(from, points.backTopLeft, lookTarget.transform, layerMask, debug))
            {
                return(true);
            }

            if (CastPoint(from, points.backTopRight, lookTarget.transform, layerMask, debug))
            {
                return(true);
            }

            if (CastPoint(from, points.backBottomLeft, lookTarget.transform, layerMask, debug))
            {
                return(true);
            }

            if (CastPoint(from, points.backBottomRight, lookTarget.transform, layerMask, debug))
            {
                return(true);
            }
        }
        return(false);
    }