Exemplo n.º 1
0
 void OnDisable()
 {
     if (targetGraphic != null)
     {
         targetGraphic.ReturnToPool();
         targetGraphic = null;
     }
 }
Exemplo n.º 2
0
    void Update()
    {
        if (Grappling) // remove the target graphic if we're grappling
        {
            if (targetGraphic)
            {
                targetGraphic.ReturnToPool();
                targetGraphic = null;
            }
        }
        else // else determine current hook
        {
            Vector3 cameraForward = cameraTransform.rotation * Vector3.forward;
            float   currentAngle  = maxLookAngle;

            // clear the current hook if
            //  - we have not been looking at it for longer than graceTime
            //  - there are colliders between the character and the hook
            if (currentHook)
            {
                Vector3 hookVector = currentHook.transform.position - cameraTransform.position;
                currentAngle = Vector3.Angle(hookVector, cameraForward);
                if (currentAngle > maxLookAngle)
                {
                    lookAwayTime += Time.deltaTime;
                    if (lookAwayTime > graceLookTime)
                    {
                        currentHook = null;
                    }
                }
                else
                {
                    lookAwayTime = 0;
                    Vector3 hookSourceVector = currentHook.transform.position - ropeEffectSource.position;
                    if (Physics.Raycast(ropeEffectSource.position, hookSourceVector,
                                        hookSourceVector.magnitude, LayerManager.I.GroundMask))
                    {
                        currentHook = null;
                    }
                }
            }

            // find all hooks potentially in range and set them to the current hook if we're looking at them and unobstructed
            var sphereCenter = cameraTransform.position + cameraTransform.rotation * Vector3.forward * (radius + minRange);
            var foundHooks   = Physics.OverlapSphere(sphereCenter, radius, LayerManager.I.HookTargets);
            foreach (var collider in foundHooks)
            {
                var testHook = collider.GetComponent <HookPoint>();
                if (testHook)
                {
                    Vector3 hookVector = testHook.transform.position - cameraTransform.position;
                    float   testAngle  = Vector3.Angle(hookVector, cameraForward);
                    if (testAngle < currentAngle)
                    {
                        Vector3 hookSourceVector = testHook.transform.position - ropeEffectSource.position;
                        if (!Physics.Raycast(ropeEffectSource.position, hookSourceVector,
                                             hookSourceVector.magnitude, LayerManager.I.GroundMask))
                        {
                            currentAngle = testAngle;
                            currentHook  = testHook;
                            lookAwayTime = 0;
                        }
                    }
                }
            }

            // create/move/remove the target graphic
            if (currentHook)
            {
                if (!targetGraphic)
                {
                    targetGraphic = targetGraphicPrefab.CreatePooledInstance <UI.UIGfxWorldObject>(GameManager.I.UIGfxCanvas);
                }
                targetGraphic.SetTracking(currentHook.transform, GameManager.I.BaseCamera);
            }
            else if (targetGraphic)
            {
                targetGraphic.ReturnToPool();
                targetGraphic = null;
            }
        }
    }