Exemple #1
0
    private void OnTriggerEnter2D(Collider2D collision)
    {
        ToyController tc = collision.GetComponent <ToyController>();

        if (tc != null)
        {
            if (!tc.isPickedUp || alsoDetectWhenPickedUp)
            {
                OnToyDetected.Invoke(tc);
            }
        }
    }
        private void OnTriggerEnter(Collider otherCollider)
        {
            if (!Detecting)
            {
                return;
            }

            var suspiciousObject = otherCollider.gameObject.GetComponent <SuspiciousObject>();

            if (suspiciousObject == null)
            {
                return;
            }

            var guard = GetComponentInParent <Guard>();

            // Set up the target and origin for the raycast.
            // The raycast goes from the player to the guard because that works
            // and the other way around it doesn't. No clue why.
            var origin = suspiciousObject.gameObject.transform.position;

            // Subtracting the origin from the target will provide the direction
            // to shoot the raycast in.
            var target = transform.parent.position - origin;

            // Raycast from the origin towards the target.
            Physics.Raycast(origin, target, out var keyCastOut);

            // If nothing was hit, no action is required.
            if (keyCastOut.transform == null)
            {
                return;
            }

            // If the developer wants to debug the raycast of the trigger, we
            // make a visible ray and also give a log message.
            if (DrawRay)
            {
                Debug.DrawRay(origin, target, RayColor, RayTime);
                Debug.Log($"Vision Raycast hit \"{keyCastOut.transform.gameObject.name}");
            }

            // If something is hit, check if it's the guard.
            // If it is, alert the guard, if not do nothing.
            if (keyCastOut.transform.gameObject == guard.gameObject)
            {
                DetectionEvent?.Invoke(suspiciousObject);

                guard.ChangeState(suspiciousObject);
            }
        }
    private void FrontCheck()
    {
        RaycastHit hit;
        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit, Mathf.Infinity))
        {
            var      detected                = hit.transform.gameObject;
            Collider detectedCollider        = detected.GetComponent <Collider>();
            Vector3  cameraPosition          = Camera.main.transform.position;
            Vector3  detectedHitClosestPoint = detectedCollider.ClosestPointOnBounds(cameraPosition);
            float    distanceToCollision     = Vector3.Distance(cameraPosition, detectedHitClosestPoint);
            ObjectFoundEvent.Invoke(hit.transform.gameObject, distanceToCollision);
        }
    }