コード例 #1
0
    private void OnTriggerEnter2D(Collider2D collision)
    {
        //Check if triggering object is a valid target
        VisibleTarget target = collision.GetComponent <VisibleTarget>();

        //If a valid target enters the detection range, add them to valid targets
        if (target && targetChoosing.priorities.Contains(target.type))
        {
            targetsInRange.Add(target);
            Debug.Log(target.gameObject.name + " entered range");
        }
    }
コード例 #2
0
    public VisibleTarget CheckForTarget()
    {
        //Scan for each eye and combine results into one hashset
        HashSet <VisibleTarget> visibleTargets = new HashSet <VisibleTarget>();

        foreach (Detection detector in detectors)
        {
            visibleTargets.UnionWith(detector.ScanForTargets());
        }

        //If current target is no longer visible, set current target to null
        if (!visibleTargets.Contains(currentTarget))
        {
            currentTarget = null;
        }

        //If no valid visible targets were found, set current target to null
        if (visibleTargets.Count == 0)
        {
            currentTarget = null;
        }
        else
        {
            //Iterate through list of target types from most to least important
            foreach (VisibleTarget.TargetType type in priorities)
            {
                //If the current target is of the type, stop iterating
                if (currentTarget && currentTarget.type == type)
                {
                    break;
                }

                //Iterate through all visible targets
                foreach (VisibleTarget target in visibleTargets)
                {
                    //Choose the first target of the type as current target and stop iteration
                    if (target.type == type)
                    {
                        currentTarget = target;
                        break;
                    }
                }
            }
        }

        return(currentTarget);
    }
コード例 #3
0
    private void OnTriggerExit2D(Collider2D collision)
    {
        //Check if triggering object is a valid target
        VisibleTarget target = collision.GetComponent <VisibleTarget>();

        //If a valid target exits the detection range, remove them from valid targets
        if (target && targetsInRange.Contains(target))
        {
            targetsInRange.Remove(target);
            Debug.Log(target.gameObject.name + " is out of range");
        }

        //Also remove them from actually visible targets
        if (target && seenTargets.Contains(target))
        {
            seenTargets.Remove(target);
        }
    }
コード例 #4
0
    // Update is called once per frame
    void Update()
    {
        if (targetCheckTimer <= 0)
        {
            currentTarget = targetChoosing.CheckForTarget();
        }
        else
        {
            targetCheckTimer -= Time.deltaTime;
        }

        if (currentTarget)
        {
            float targetDirection = -Mathf.Sign(transform.position.x - currentTarget.transform.position.x);
            movement.SetVelocityX(targetDirection, true);
        }
        else
        {
            movement.SetVelocityX(0, true);
        }
    }