Пример #1
0
        Vector3 LerpToTarget()
        {
            Vector3    desiredPosition = transform.position;
            CameraZone zone            = (cameraZones.Count > 0) ? cameraZones[cameraZones.Count - 1] : null;

            Rigidbody2D r = target.GetComponent <Rigidbody2D>();
            Vector2     v = (r == null)? Vector2.zero: Vector2.Scale(r.velocity, (Vector2)velocityAdjustment);
            float       d = Vector3.Distance(target.position, transform.position + offset);

            if (d > moveStartDist)
            {
                Vector3 targetPosition = Vector3.Lerp(
                    transform.position,
                    target.position + offset + (Vector3)v - Vector3.forward * v.magnitude * velocityAdjustment.z,
                    Time.deltaTime * speed
                    );
                if (zone == null)
                {
                    desiredPosition = targetPosition;
                }
                else
                {
                    desiredPosition = zone.position.Lerp3(targetPosition, zone.axisLock);
                }
            }

            return(desiredPosition);
        }
Пример #2
0
        void SetPosition()
        {
            GameObject go = GameObject.FindWithTag("Player");

            Collider2D[] colliders = Physics2D.OverlapPointAll(go.transform.position);

            if (target == null)
            {
                target = go.transform;
            }
            transform.position = target.position + offset;
            foreach (Collider2D collider in colliders)
            {
                CameraZone zone = collider.gameObject.GetComponent <CameraZone>();
                if (zone != null)
                {
                    transform.position = zone.position.Lerp3(transform.position, zone.axisLock);
                    return;
                }
            }
            if (bounded)
            {
                transform.position = camera.GetBoundedPos(bounds);
            }
        }
Пример #3
0
        IEnumerator TransitionToZoneCoroutine()
        {
            transitioning = true;
            CameraZone zone = cameraZones[cameraZones.Count - 1];

            if (zone.music != null)
            {
                AudioManager.CrossfadeMusic(zone.music, zone.transitionTime);
            }
            Vector3 pos         = transform.position;
            float   elapsedTime = 0;

            while (elapsedTime < zone.transitionTime)
            {
                float dT = Time.deltaTime;
                if (zone.useUnscaledTime)
                {
                    dT = Time.unscaledDeltaTime;
                }
                elapsedTime += dT;
                Vector3 dir = zone.position - transform.position;
                float   t   = zone.transitionTime - elapsedTime;
                pos += dir * dT / t;
                transform.position = Vector3.Lerp(transform.position, pos, dT * speed);
                yield return(new WaitForEndOfFrame());
            }
            rawPosition   = transform.position;
            transitioning = false;
        }
Пример #4
0
 public static void AddZone(CameraZone zone)
 {
     if (instance.cameraZones.Contains(zone))
     {
         return;
     }
     instance.cameraZones.Add(zone);
     instance.StopCoroutine("TransitionToZoneCoroutine");
     instance.StartCoroutine("TransitionToZoneCoroutine");
 }
Пример #5
0
 public void AddZone(CameraZone zone)
 {
     if (cameraZones.Contains(zone))
     {
         return;
     }
     cameraZones.Add(zone);
     StopCoroutine("TransitionToZoneCoroutine");
     StartCoroutine("TransitionToZoneCoroutine");
 }
Пример #6
0
 public static void RemoveZone(CameraZone zone)
 {
     instance.cameraZones.Remove(zone);
     instance.StopCoroutine("TransitionToZoneCoroutine");
     if (instance.cameraZones.Count > 0)
     {
         instance.StartCoroutine("TransitionToZoneCoroutine");
     }
     else if (instance.defaultMusic != null)
     {
         AudioManager.CrossfadeMusic(instance.defaultMusic, 0.5f);
     }
 }
Пример #7
0
        void SetPosition()
        {
            if (target == null)
            {
                GameObject go = GameObject.FindWithTag(targetTag);
                if (go == null)
                {
                    return;
                }
                target = go.transform;
            }
            Vector3 off = offset;

            if (instances.Length > 1)
            {
                off = splitOffset;
            }
            transform.position = target.position + off;
            rawPosition        = transform.position;

            Collider2D[] collider2Ds = Physics2D.OverlapPointAll(target.position);
            foreach (Collider2D collider2D in collider2Ds)
            {
                CameraZone zone = collider2D.gameObject.GetComponent <CameraZone>();
                if (zone != null)
                {
                    transform.position = zone.position.Lerp3(transform.position, zone.axisLock);
                    break;
                }
            }

            Collider[] colliders = Physics.OverlapSphere(target.position, 1);
            foreach (Collider collider in colliders)
            {
                CameraZone zone = collider.gameObject.GetComponent <CameraZone>();
                if (zone != null)
                {
                    transform.position = zone.position.Lerp3(transform.position, zone.axisLock);
                    break;
                }
            }

            if (bounded)
            {
                BoundPosition();
            }
        }
Пример #8
0
        void SetPosition()
        {
            GameObject go = GameObject.FindWithTag(targetTag);

            if (go == null)
            {
                return;
            }

            if (target == null)
            {
                target = go.transform;
            }
            transform.position = target.position + offset;

            Collider2D[] collider2Ds = Physics2D.OverlapPointAll(go.transform.position);
            foreach (Collider2D collider2D in collider2Ds)
            {
                CameraZone zone = collider2D.gameObject.GetComponent <CameraZone>();
                if (zone != null)
                {
                    transform.position = zone.position.Lerp3(transform.position, zone.axisLock);
                    return;
                }
            }


            Collider[] colliders = Physics.OverlapSphere(go.transform.position, 1);
            foreach (Collider collider in colliders)
            {
                CameraZone zone = collider.gameObject.GetComponent <CameraZone>();
                if (zone != null)
                {
                    transform.position = zone.position.Lerp3(transform.position, zone.axisLock);
                    return;
                }
            }
            if (bounded)
            {
                if (boundsObject)
                {
                    bounds = boundsObject.RendererBounds();
                }
                transform.position = camera.GetBoundedPos(bounds);
            }
        }
Пример #9
0
 public void RemoveZone(CameraZone zone)
 {
     cameraZones.Remove(zone);
     transitioning = false;
     StopCoroutine("TransitionToZoneCoroutine");
     if (cameraZones.Count > 0)
     {
         StartCoroutine("TransitionToZoneCoroutine");
     }
     else
     {
         rawPosition = transform.position;
         if (defaultMusic != null)
         {
             AudioManager.CrossfadeMusic(instance.defaultMusic, 0.5f);
         }
     }
 }
Пример #10
0
        IEnumerator TransitionToZoneCoroutine()
        {
            CameraZone zone = cameraZones[cameraZones.Count - 1];

            if (zone.music != null)
            {
                AudioManager.CrossfadeMusic(zone.music, zone.transitionTime);
            }
            Vector3 startPos = transform.position;
            float   t        = 0;

            while (t < zone.transitionTime)
            {
                t += Time.deltaTime;
                float frac = t / zone.transitionTime;
                transform.position = Vector3.Lerp(startPos, zone.position, frac);
                yield return(new WaitForEndOfFrame());
            }
            transform.position = zone.position;
        }
Пример #11
0
        void UpdatePosition()
        {
            if (target != null && camera != null)
            {
            #if UNITY_EDITOR
                if (!Application.isPlaying)
                {
                    SetPosition();
                }
                else
                {
            #endif
                if (!transitioning)
                {
                    LerpToTarget();

                    Vector3 mergedPosition = rawPosition;
                    Vector3 diff           = center - rawPosition;
                    float   dist           = diff.magnitude;
                    if (dist < mergeDistance)
                    {
                        _merge         = 1;
                        mergedPosition = center;
                    }
                    else if (dist < mergeStartDistance)
                    {
                        _merge         = (mergeStartDistance - dist) / (mergeStartDistance - mergeDistance);
                        mergedPosition = Vector3.Lerp(rawPosition, center, _merge);
                    }
                    else
                    {
                        _merge = 0;
                    }

                    CameraZone zone = (cameraZones.Count > 0) ? cameraZones[cameraZones.Count - 1] : null;
                    if (zone != null)
                    {
                        mergedPosition = zone.position.Lerp3(mergedPosition, zone.axisLock);
                    }

                    transform.position = mergedPosition;
                }

                if (bounded)
                {
                    BoundPosition();
                }
            #if UNITY_EDITOR
            }
            #endif
            }
            else
            {
                GameObject g = GameObject.FindWithTag(targetTag);
                if (g != null)
                {
                    target = g.transform;
                    SetPosition();
                }
            }
        }