Beispiel #1
0
    void LateUpdate()
    {
        focusArea.Update(m_target.collider.bounds);

        Vector2 focusPosition = focusArea.centre + Vector2.up * verticalOffset;

        if (focusArea.velocity.x != 0)
        {
            lookAheadDirX = Mathf.Sign(focusArea.velocity.x);
            if (Mathf.Sign(m_target.playerInput.x) == Mathf.Sign(focusArea.velocity.x) && m_target.playerInput.x != 0)
            {
                lookAheadStopped = false;
                targetLookAheadX = lookAheadDirX * lookAheadDstX;
            }
            else
            {
                if (!lookAheadStopped)
                {
                    lookAheadStopped = true;
                    targetLookAheadX = currentLookAheadX + (lookAheadDirX * lookAheadDstX - currentLookAheadX) / 4f;
                }
            }
        }


        currentLookAheadX = Mathf.SmoothDamp(currentLookAheadX, targetLookAheadX, ref smoothLookVelocityX, lookSmoothTimeX);

        focusPosition.y = Mathf.SmoothDamp(transform.position.y, focusPosition.y, ref smoothVelocityY, verticalSmoothTime);
        focusPosition  += Vector2.right * currentLookAheadX;
        Vector3 newPos = (Vector3)focusPosition + Vector3.forward * depthOffset;

        transform.position = new Vector3(Mathf.Lerp(transform.position.x, newPos.x, horizontalSmoothTime), newPos.y, newPos.z);
    }
Beispiel #2
0
    void LateUpdate()
    {
        focusArea.Update(target.GetComponent <BoxCollider2D>().bounds);
        Vector2 focusPosition = (focusArea.centre) + Vector2.up * vericalOffset;

        transform.position = ((Vector3)focusPosition + Vector3.right * horizontalOffset) + Vector3.forward * -10;
    }
Beispiel #3
0
    void LateUpdate()     //spätes Update, alle anderen updates vom player movement etc. sind schon fertig
    {
        focusArea.Update(target.GetComponent <Collider2D>().bounds);


        Vector2 focusPosition = focusArea.centre + Vector2.up * verticalOffSet;

        if (focusArea.velocity.x != 0)
        {
            lookAheadDirectionX = Mathf.Sign(focusArea.velocity.x);
            if (Mathf.Sign(target.RawInput.x) == Mathf.Sign(focusArea.velocity.x) && target.RawInput.x != 0)
            {
                lookAheadStopped       = false;
                targetSmoothLookAheadX = lookAheadDirectionX * lookAheadX;
            }
            else
            {
                if (!lookAheadStopped)
                {
                    lookAheadStopped       = true;
                    targetSmoothLookAheadX = currentLookAheadX + (lookAheadDirectionX * lookAheadX - currentLookAheadX) / 4;
                }
            }
        }


        currentLookAheadX = Mathf.SmoothDamp(currentLookAheadX, targetSmoothLookAheadX, ref smoothLookVelocityX, smoothTimeX);

        focusPosition.y = Mathf.SmoothDamp(transform.position.y, focusPosition.y, ref smoothLookVelocityY, smoothTimeY);

        focusPosition += Vector2.right * currentLookAheadX;

        transform.position = (Vector3)focusPosition + Vector3.forward * -10;
    }
Beispiel #4
0
    private void LateUpdate()
    {
        focusArea.Update(target.collider.bounds);
        Vector2 focusPosition = focusArea.center + Vector2.up * verticalOffset;

        if (focusArea.velocity.x != 0)
        {
            lookAheadDirX = Mathf.Sign(focusArea.velocity.x);
            if (Mathf.Sign(target.playerInput.x) == Mathf.Sign(focusArea.velocity.x) && target.playerInput.x != 0)
            {
                targetLookAheadX = lookAheadDirX * lookAheadDstX;
                lookAheadStopped = false;
            }
            else
            {
                if (!lookAheadStopped)
                {
                    lookAheadStopped = true;
                    targetLookAheadX = currentLookAheadX + (lookAheadDirX * lookAheadDstX - currentLookAheadX) / 4f;
                }
            }
        }

        currentLookAheadX = Mathf.SmoothDamp(currentLookAheadX, targetLookAheadX, ref smoothLookVelocityX, lookSmoothTimeX);

        focusPosition.y    = Mathf.SmoothDamp(transform.position.y, focusPosition.y, ref smoothVelocityY, verticalSmoothTime);
        focusPosition     += Vector2.right * currentLookAheadX;
        transform.position = (Vector3)focusPosition + Vector3.forward * -10;
    }
Beispiel #5
0
    void LateUpdate()
    {
        if (upgradesFlag)
        {
            upgradesFlag = false;
            MoveToUpgrades();
        }
        if (gameFlag)
        {
            gameFlag = false;
            MoveToGame();
        }

        if (isNeeded && target != null)
        {
            focusArea.Update(target.bounds);

            Vector3 focusPosition = new Vector3(focusArea.centre.x, cameraYZ.x, cameraYZ.y + focusArea.centre.z); // + Vector3.up * verticalOffset;

            transform.position = focusPosition;
        }

        if (timerDown < Time.time && goDown)
        {
            goDown = false;
            //upgradeGroup.gameObject.SetActive(false);
            FindObjectOfType <Spawner>().NextWave();
        }
    }
Beispiel #6
0
    void LateUpdate()
    {
        focusArea.Update(target.collider.bounds);

        Vector2 focusPosition = focusArea.center + Vector2.up * verticalOffset;

        if (focusArea.velocity.x != 0)
        {
            // Set direction
            lookAheadDirectionX = Mathf.Sign(focusArea.velocity.x);
            if (Mathf.Sign(target.playerInput.x) == Mathf.Sign(focusArea.velocity.x) && target.playerInput.x != 0)
            {
                lookAheadStopped = false;
                targetLookAheadX = lookAheadDirectionX * lookAheadDistanceX;
            }
            else
            {
                if (!lookAheadStopped)
                {
                    lookAheadStopped = true;
                    targetLookAheadX = currentLookAheadX + (lookAheadDirectionX * lookAheadDistanceX - currentLookAheadX) / 4.0f;   // 4 is an arbitrary fraction of the distance to look ahead
                }
            }
        }


        currentLookAheadX = Mathf.SmoothDamp(currentLookAheadX, targetLookAheadX, ref smoothLookVelocityX, lookSmoothTimeX);

        focusPosition.y = Mathf.SmoothDamp(transform.position.y, focusPosition.y, ref smoothVelocityY, verticalSmoothTime);
        focusPosition  += Vector2.right * currentLookAheadX;

        transform.position = (Vector3)focusPosition + Vector3.forward * -10;
    }
Beispiel #7
0
    private void LateUpdate()
    {
        focusArea.Update(target.collider.bounds);

        Vector2 focusPosition = focusArea.center + Vector2.up * verticalOffset;

        //This if statement depends on there being some residual decceleration when stopping. If we stop instantly then the incremental camera pan won't work
        if (focusArea.velocity.x != 0)
        {
            lookAheadDirectionX = Mathf.Sign(focusArea.velocity.x); //Get the direction the camera is moving
            //Check if the player is attempting to move in the same direction that the camera needs to move
            if (Mathf.Sign(target.playerInput.x) == Mathf.Sign(focusArea.velocity.x) && target.playerInput.x != 0)
            {
                lookAheadStopped = false;
                targetLookAheadX = lookAheadDirectionX * lookAheadDistanceX;
            }
            else
            {
                if (!lookAheadStopped) //If the player released the input mid camera pan, only adjust it slightly as to not feel "jerky"
                {
                    lookAheadStopped = true;
                    targetLookAheadX = currentLookAheadX + (lookAheadDirectionX * lookAheadDistanceX - currentLookAheadX) / 4f;
                }
            }
        }
        //Smooth the horizontal camera movement
        currentLookAheadX = Mathf.SmoothDamp(currentLookAheadX, targetLookAheadX, ref smoothLookVelocityX, lookSmoothTimeX);

        //Smooth and set the vertical camera movement component
        focusPosition.y = Mathf.SmoothDamp(transform.position.y, focusPosition.y, ref smoothVelocityY, verticalSmoothTime);

        //Add the smoothed horizontal camera movement
        focusPosition += Vector2.right * currentLookAheadX;

        checkPosition = (Vector3)focusPosition + Vector3.forward * -10;


        //Make sure we don't let the camera view past the bounds of the level
        if (checkPosition.x <= minCameraPositionX)
        {
            focusPosition.x = minCameraPositionX;
        }
        else if (checkPosition.x >= maxCameraPositionX)
        {
            focusPosition.x = maxCameraPositionX;
        }

        if (checkPosition.y <= minCameraPositionY)
        {
            focusPosition.y = minCameraPositionY;
        }
        else if (checkPosition.y >= maxCameraPositionY)
        {
            focusPosition.y = maxCameraPositionY;
        }

        //Apply the movement to the camera. Vector3.forward * -10 is to keep the camera in front of the scene on the z-axis
        transform.position = (Vector3)focusPosition + Vector3.forward * -10;
    }
    void LateUpdate()
    {
        focusArea.Update(target.GetComponent <BoxCollider2D>().bounds);

        Vector2 focusPosition = focusArea.Centre + Vector2.up * verticalOffset;

        focusPosition.y    = Mathf.SmoothDamp(transform.position.y, focusPosition.y, ref smoothVelocityY, verticalSmoothTime);
        transform.position = new Vector3(focusPosition.x, focusPosition.y, -10);
    }
    // all player movement is finished for frame
    private void LateUpdate()
    {
        // update camera focus area based on Player collider
        focusArea.Update(target.GetComponent <BoxCollider2D>().bounds);

        // move camera up from player by a certain offset
        Vector2 focusPosition = focusArea.center + Vector2.up * verticalOffset;

        Vector3 temp = transform.position;

        // change camera position based on focusPosition
        transform.position = (Vector3)focusPosition + Vector3.forward * -10; // camera is always infront of play area
        Camera cam = GetComponent <Camera>();

        if (transform.position.x < minMapX)
        {
            Vector3 newpos;
            newpos.x           = minMapX;
            newpos.y           = transform.position.y;
            newpos.z           = transform.position.z;
            transform.position = newpos;
        }

        if (transform.position.x + cam.orthographicSize > maxMapX)
        {
            Vector3 newpos;
            newpos.x           = maxMapX - cam.orthographicSize;
            newpos.y           = transform.position.y;
            newpos.z           = transform.position.z;
            transform.position = newpos;
        }

        if (transform.position.y + cam.orthographicSize > maxMapY)
        {
            Vector3 newpos;
            newpos.x           = transform.position.x;
            newpos.y           = maxMapY - cam.orthographicSize;
            newpos.z           = transform.position.z;
            transform.position = newpos;
        }

        if (transform.position.y < minMapY)
        {
            Vector3 newpos;
            newpos.x           = transform.position.x;
            newpos.y           = minMapY;
            newpos.z           = transform.position.z;
            transform.position = newpos;
        }
    }
Beispiel #10
0
    // used by cameras because takes place after movement in Update/FixedUpdate
    private void LateUpdate()
    {
        focusArea.Update(target.collider.bounds);

        // moving camera a little further from focus area
        Vector2 focusPosition = focusArea.center + Vector2.up * verticalOffset;

        // only look ahead if player is moving
        if (focusArea.velocity.x != 0)
        {
            // direction indicated by sign of focusArea's velocity
            lookAheadDirectionX = Mathf.Sign(focusArea.velocity.x);

            // player has some degree of smoothing. When player is moving left, if tries moving right
            // player will continue moving left as it slows down, then starts moving right
            // only want to set look ahead in x direction if camera is looking in same direction as
            // focus area is moving
            // Sign returns 1 for 0 so must check player input is not 0
            if (Mathf.Sign(target.playerInput.x) == Mathf.Sign(focusArea.velocity.x) &&
                target.playerInput.x != 0)
            {
                lookAheadStopped = false;
                // look ahead is distance multiplied by direction to move look in right direction
                targetLookAheadX = lookAheadDirectionX * lookAheadDistanceX;
            }
            else
            {
                if (!lookAheadStopped)
                {
                    lookAheadStopped = true;
                    // don't have lookaheadx go much further but don't want to stop it daed
                    // add just a fraction of remaining distance to achieve this
                    targetLookAheadX = currentLookAheadX + (lookAheadDirectionX * lookAheadDistanceX - currentLookAheadX) / 4f;
                }
            }
        }

        // smooth in x direction toward target look ahead position in time smoothing is set to take
        currentLookAheadX = Mathf.SmoothDamp(currentLookAheadX, targetLookAheadX, ref smoothLookVelocityX, lookSmoothTimeX);
        // vertical smoothing
        focusPosition.y = Mathf.SmoothDamp(transform.position.y, focusPosition.y, ref smoothVelocityY, verticalSmoothTime);
        focusPosition  += Vector2.right * currentLookAheadX;

        // move camera
        // multiply by -10 to make sure camera is always in front of scene
        transform.position = (Vector3)focusPosition + Vector3.forward * -10;
    }
Beispiel #11
0
    private void LateUpdate()
    {
        _focusArea.Update(Target.bounds);
        _focusPosition = _focusArea.Center + Vector2.up * VerticalOffset;

        if (_focusArea.Velocity.x != 0)
        {
            _lookAheadDirX = Mathf.Sign(_focusArea.Velocity.x);
        }

        _targetLookAheadX  = _lookAheadDirX * LookAheadDstX;
        _currentLookAheadX = Mathf.SmoothDamp(_currentLookAheadX, _targetLookAheadX, ref _smoothLookVelocityX, LookSmoothX);

        _focusPosition += Vector2.right * _currentLookAheadX;

        transform.position = (Vector3)_focusPosition + Vector3.forward * _offsetZ;
    }
Beispiel #12
0
    /// <summary>
    /// Runs after the other update methods.
    ///
    /// great for camera movement
    /// </summary>
    private void LateUpdate()
    {
        // Calling the focus area update with the target's bounds
        focusArea.Update(target._boxCol.bounds);

        // Defines the focus area posiion
        Vector2 focusPosition = focusArea.center + Vector2.up * verticalOffset;

        // velocity of focus area of the x axis is not 0
        //  Set look ahead direction equal to the sign of focusArea.velocity.x
        if (focusArea.velocity.x != 0)
        {
            lookAheadDirX = Mathf.Sign(focusArea.velocity.x);

            // For smoothing out camera when dealing with left and right player input
            if (Mathf.Sign(target._playerInput.x) == Mathf.Sign(focusArea.velocity.x) && target._playerInput.x != 0)
            {
                lookAheadStopped = false;

                // Sets target look ahead to the full ammount
                targetLookAheadX = lookAheadDirX * lookAheadDstX;
            }
            else
            {
                if (!lookAheadStopped)
                {
                    lookAheadStopped = true;
                    targetLookAheadX = currentLookAheadX + (lookAheadDirX * lookAheadDstX - currentLookAheadX) / 4;
                }
            }
        }

        // Sets the current lookahead X
        currentLookAheadX = Mathf.SmoothDamp(currentLookAheadX, targetLookAheadX, ref smoothLookVelocityX, lookSmoothTimeX);

        // Y smoothing
        focusPosition.y = Mathf.SmoothDamp(transform.position.y, focusPosition.y, ref smoothVelocityY, verticalSmoothTime);

        // Sets the new focus position ahead of the player
        focusPosition += Vector2.right * currentLookAheadX;



        // Moves the camera when the player touches the side of the bounding box
        transform.position = (Vector3)focusPosition + Vector3.forward * -10;
    }
Beispiel #13
0
    void LateUpdate()
    {
        focusArea.Update(target.collider.bounds);

        Vector2 focusPosition = focusArea.centre + Vector2.up * verticalOffset;

        if (focusArea.velocity.x != 0)
        {
            lookAheadDirX = Mathf.Sign(focusArea.velocity.x);
        }

        targetLookAheadX = lookAheadDirX * lookAheadDstX;

        currentLookAheadX = Mathf.SmoothDamp(currentLookAheadX, targetLookAheadX, ref smoothLookVelocityX, lookSmoothTimeX);

        focusPosition += Vector2.right * currentLookAheadX;

        transform.position = (Vector3)focusPosition + Vector3.forward * -10;
    }
Beispiel #14
0
    private void LateUpdate()
    {
        if (!finalLevelUnlocked || !firstTimeToHubSinceFinalLevelUnlocked)
        {
            focusArea.Update(target.bounds);

            Vector2 focusPosition = focusArea.center + Vector2.up * verticalOffset;

            if (focusArea.velocity.x != 0)
            {
                lookAheadDirX    = Mathf.Sign(focusArea.velocity.x);
                lookAheadStopped = false;
                targetLookAheadX = lookAheadDirX * lookAheadDstX;
            }
            else
            {
                if (!lookAheadStopped)
                {
                    lookAheadStopped = true;
                    targetLookAheadX = currentLookAheadX + (lookAheadDirX * lookAheadDstX - currentLookAheadX) / 4;
                }
            }

            currentLookAheadX = Mathf.SmoothDamp(currentLookAheadX, targetLookAheadX, ref smoothLookVelocityX, lookSmoothTimeX);

            focusPosition.y = Mathf.SmoothDamp(transform.position.y, focusPosition.y, ref smoothVelocityY, verticalSmoothTime);
            focusPosition  += Vector2.right * currentLookAheadX;

            if (float.IsNaN(focusPosition.x) || float.IsNaN(focusPosition.y))
            {
                if (WLD_GameController.player.activeSelf)
                {
                    focusPosition = new Vector2(WLD_GameController.player.transform.position.x, WLD_GameController.player.transform.position.y + 2.5f);
                }
                else
                {
                    focusPosition = new Vector2(0, 2.5f);
                }
            }

            transform.position = (Vector3)focusPosition + Vector3.forward * zCamDist;
        }
    }
Beispiel #15
0
    void LateUpdate()
    {
        if (target == null)
        {
            target    = GameObject.FindWithTag("Player").GetComponent <Controller2D>();
            focusArea = new FocusArea(target.collider.bounds, focusAreaSize);
            return;
        }

        focusArea.Update(target.collider.bounds);

        Vector2 focusPosition = focusArea.center + Vector2.up * verticalOffset;

        if (focusArea.velocity.x != 0)
        {
            lookAheadDirX = Mathf.Sign(focusArea.velocity.x);
            if (Mathf.Sign(controllerData.inputDirection.x) == Mathf.Sign(focusArea.velocity.x) && controllerData.inputDirection.x != 0)
            {
                lookAheadStopped = false;
                targetLookAheadX = lookAheadDirX * lookAheadDstX;
            }
            else
            {
                if (lookAheadStopped == false)
                {
                    lookAheadStopped = true;
                    targetLookAheadX = currentLookAheadX + (lookAheadDirX * lookAheadDstX - currentLookAheadX) / 4f;
                }
            }
        }

        currentLookAheadX = Mathf.SmoothDamp(currentLookAheadX, targetLookAheadX, ref smoothLookVelocityX, lookSmoothTimeX);

        focusPosition += Vector2.right * currentLookAheadX;
        Vector3 newPosition = (Vector3)focusPosition + Vector3.forward * -10;

        newPosition.x = Mathf.Clamp(newPosition.x, this.clampCameraX.x, this.clampCameraX.y);
        newPosition.y = Mathf.Clamp(newPosition.y, this.clampCameraY.x, this.clampCameraY.y);

        transform.position = newPosition;
    }
Beispiel #16
0
    void LateUpdate()
    {
        FocusArea.Update(Target.Collider2D.bounds);

        Vector2 focusPosition = FocusArea.Centre + Vector2.up * VerticalOffset;

        if (FocusArea.Velocity.x != 0)
        {
            LookAheadDirX = Mathf.Sign(FocusArea.Velocity.x);
            if (Mathf.Sign(Target.PlayerInput.x) == Mathf.Sign(FocusArea.Velocity.x) && Target.PlayerInput.x != 0)
            {
                LookAheadStopped = false;
                TargetLookAheadX = LookAheadDirX * LookAheadDstX;
            }
            else
            {
                // If the player stopped moving in the X axis, the camera will stop the look ahead feature.
                if (!LookAheadStopped)
                {
                    LookAheadStopped = true;
                    TargetLookAheadX = CurrentLookAheadX + (LookAheadDirX * LookAheadDstX - CurrentLookAheadX) / 4f;
                }
            }
        }

        // Temp variable to store the SmoothDamp float, we assign it to the desired variable in the next few lines.
        float tempSLVX = SmoothLookVelocityX;

        CurrentLookAheadX   = Mathf.SmoothDamp(CurrentLookAheadX, TargetLookAheadX, ref tempSLVX, LookSmoothTimeX);
        SmoothLookVelocityX = tempSLVX;

        // Temp variable to store the SmoothDamp float, we assign it to the desired variable in the next few lines.
        float tempSVY = SmoothVelocityY;

        focusPosition.y = Mathf.SmoothDamp(transform.position.y, focusPosition.y, ref tempSVY, VerticalSmoothTime);
        SmoothVelocityY = tempSVY;

        focusPosition     += Vector2.right * CurrentLookAheadX;
        transform.position = (Vector3)focusPosition + Vector3.forward * -10;
    }
    private void LateUpdate()
    {
        if (target != null)
        {

            focusArea.Update(target.m_collider.bounds);

            Vector2 focusPosition = focusArea.centre + Vector2.up * verticalOffset;

            if (focusArea.velocity.x != 0)
            {
                lookAheadDirX = Mathf.Sign(focusArea.velocity.x);
                if (Mathf.Sign(target.PlayerInput.x) == Mathf.Sign(focusArea.velocity.x) && target.PlayerInput.x != 0)
                {
                    lookAheadStopped = false;
                    targetLookAheadX = lookAheadDirX * lookAheadDstX;
                }
                else
                {
                    if (!lookAheadStopped)
                    {
                        lookAheadStopped = true;
                        targetLookAheadX = currentLookAheadX + (lookAheadDirX * lookAheadDstX - currentLookAheadX) / 4f;
                    }
                }
            }

            currentLookAheadX = Mathf.SmoothDamp(currentLookAheadX, targetLookAheadX, ref smoothLookVelocityX, lookSmoothTimeX);

            focusPosition.y = Mathf.SmoothDamp(transform.position.y, focusPosition.y, ref smoothVelocityY, verticalSmoothTime);
            focusPosition += Vector2.right * currentLookAheadX;

            currentCamPositionX = (Vector3)focusPosition + Vector3.forward * -10;
            currentCamPositionX.x = Mathf.Clamp(currentCamPositionX.x, xMin, xMax);

            //aqui se asigna el valor
            transform.position = currentCamPositionX;
        }
    }
    void LateUpdate()
    {
        // update focus area position
        focusArea.Update(target.bounds);

        // get lookahead based on the current velocity
        if (focusArea.velocity.x != 0f)
        {
            lookAheadDir = Mathf.Sign(focusArea.velocity.x);
        }

        targetLookAhead  = lookAheadDir * lookAhead;
        currentLookAhead = Mathf.SmoothDamp(currentLookAhead, targetLookAhead, ref lookAheadSmoothing, hSmoothDelay);

        // update focus area position by lookahead and vertical offset
        Vector2 focusPosition = focusArea.center + Vector2.up * vOffset;

        focusPosition += Vector2.right * currentLookAhead;

        // update camera position
        transform.position = (Vector3)focusPosition + Vector3.forward * camOffset;
    }
Beispiel #19
0
    void LateUpdate()
    {
        focusArea.Update(target.coll2D.bounds);

        Vector2 focusPosition = focusArea.centre + Vector2.up * verticalOffset;

        if (focusArea.velocity.x != 0)
        {
            lookAheadDirX = Mathf.Sign(focusArea.velocity.x);
            if (Mathf.Sign(target.playerInput.x) == Mathf.Sign(focusArea.velocity.x) && target.playerInput.x != 0)
            {
                lookAheadStopped = false;
                targetLookAheadX = lookAheadDirX * lookAheadDstX;
            }
            else
            {
                if (!lookAheadStopped)
                {
                    lookAheadStopped = true;
                    targetLookAheadX = currentLookAheadX + (lookAheadDirX * lookAheadDstX - currentLookAheadX) / 4f;
                }
            }
        }

        currentLookAheadX = Mathf.SmoothDamp(currentLookAheadX, targetLookAheadX, ref smoothlookVelocityX, lookSmoothTimeX);

        focusPosition.y    = Mathf.SmoothDamp(transform.position.y, focusPosition.y, ref smoothVelocityY, verticalSmoothTime);
        focusPosition     += Vector2.right * currentLookAheadX;
        transform.position = (Vector3)focusPosition + Vector3.forward * -10;

        if (this.isShake)
        {
            if (this.timeStartShake + this.timeToShake < Time.time)
            {
                this.isShake = false;
            }
            transform.position += Random.insideUnitSphere / 16;
        }
    }
Beispiel #20
0
    // LateUpdate is called once per frame only after Update has been called because the camera tracks objects that may have moved in Update
    void LateUpdate()
    {
        if (control)
        {
            focusArea.Update(player.collider.bounds);

            Vector2 focusPosition = focusArea.center + Vector2.up * verticalOffset;

            if (focusArea.velocity.x != 0)
            {
                lookAheadDirectionX = Mathf.Sign(focusArea.velocity.x);

                // Only want to set lookAheadX if the input is the same direction as the focusArea is moving
                if ((Mathf.Sign(player.playerInput.x) == Mathf.Sign(focusArea.velocity.x)) && player.playerInput.x != 0)
                {
                    lookAheadStopped = false;
                    playerLookAheadX = lookAheadDirectionX * lookAheadDistanceX;
                }
                else
                {
                    // Allows us to stop lookAhead prematurely when the character stops
                    if (!lookAheadStopped)
                    {
                        playerLookAheadX = currentLookAheadX + (lookAheadDirectionX * lookAheadDistanceX - currentLookAheadX) / 4f;
                        lookAheadStopped = true;
                    }
                }
            }

            // Smoothing
            currentLookAheadX = Mathf.SmoothDamp(currentLookAheadX, playerLookAheadX, ref smoothLookVelocityX, lookSmoothTimeX);
            focusPosition.y   = Mathf.SmoothDamp(transform.position.y, focusPosition.y, ref smoothVelocityY, verticalSmoothTime);

            focusPosition += Vector2.right * currentLookAheadX;

            transform.position = (Vector3)focusPosition + Vector3.forward * -10;
        }
    }
Beispiel #21
0
        private void LateUpdate()
        {
            m_FocusArea.Update(MainTarget.Collider.bounds);

            m_TargetVelocityX = MainTarget.transform.position.x - m_TargetPositionX;

            if (m_FocusArea.Velocity.x != 0.0)
            {
                m_TargetHorizontalOffset += m_FocusArea.Velocity.x;
                m_TargetHorizontalOffset  =
                    Mathf.Clamp(m_TargetHorizontalOffset, -m_HorizontalMaxOffset, m_HorizontalMaxOffset);
            }

            m_CurrHorizontalOffset =
                Mathf.Lerp(m_CurrHorizontalOffset, m_TargetHorizontalOffset, Time.deltaTime * m_HorizontalSmoothSpeed);

            Vector2 focusPosition =
                m_FocusArea.Center + Vector2.up * m_VerticalOffset + Vector2.right * m_CurrHorizontalOffset;

            transform.position = (Vector3)focusPosition + Vector3.forward * -10.0f;

            m_TargetPositionX = MainTarget.transform.position.x;
        }
Beispiel #22
0
    void LateUpdate()
    {
        if (target != null)
        {
            focusArea.Update(target.GetComponent <BoxCollider2D>().bounds);
        }

        Vector2 focusPosition = focusArea.centre + Vector2.up * verticalOffset;

        if (focusArea.velocity.x != 0)
        {
            lookAheadDirX = Mathf.Sign(focusArea.velocity.x);
            if (Mathf.Sign(Input.GetAxis("Horizontal")) == Mathf.Sign(focusArea.velocity.x) && Input.GetAxis("Horizontal") != 0)
            {
                lookAheadStopped = false;
                targetLookAheadX = lookAheadDirX * lookAheadDstX;
            }
            else
            {
                if (!lookAheadStopped)
                {
                    lookAheadStopped = true;
                    targetLookAheadX = currentLookAheadX + (lookAheadDirX * lookAheadDstX - currentLookAheadX) / 4f;
                }
            }
        }


        currentLookAheadX = Mathf.SmoothDamp(currentLookAheadX, targetLookAheadX, ref smoothLookVelocityX, lookSmoothTimeX);

        focusPosition.y    = Mathf.SmoothDamp(transform.position.y, focusPosition.y, ref smoothVelocityY, verticalSmoothTime);
        focusPosition     += Vector2.right * currentLookAheadX;
        transform.position = (Vector3)focusPosition + Vector3.forward * -10;

        clampCam();
    }
Beispiel #23
0
 void LateUpdate()
 {
     focusArea.Update(target.collider.bounds);
 }
Beispiel #24
0
    void LateUpdate()
    {
        //if we have a player for the camera to target
        if (target ?? false)
        {
            //update the focus area's position.
            focusArea.Update(target.collider.bounds);

            //set the position for the camera to center on
            Vector2 focusPosition = focusArea.center + Vector2.up * verticalOffset;

            //look ahead
            if (focusArea.velocity.x != 0)
            {
                lookAheadDirX = Mathf.Sign(focusArea.velocity.x);

                if (Mathf.Sign(target.playerInput.x) == Mathf.Sign(focusArea.velocity.x) && target.playerInput.x != 0)
                {
                    lookAheadStopped = false;
                    targetLookAheadX = lookAheadDirX * lookAheadDstX;
                }
                else
                {
                    if (!lookAheadStopped)
                    {
                        lookAheadStopped = true;
                        targetLookAheadX = currentLookAheadX + (lookAheadDirX * lookAheadDstX - currentLookAheadX) / 4f;
                    }
                }
            }//end if
            targetLookAheadX  = lookAheadDirX * lookAheadDstX;
            currentLookAheadX = Mathf.SmoothDamp(currentLookAheadX, targetLookAheadX, ref smoothLookVelocityX, lookSmoothTimeX);

            //this line would smooth the Y axis movement - we don't want that for now.
            //focusPosition.y = Mathf.SmoothDamp(transform.position.y, focusPosition.y, ref smoothVelocityY, verticalSmoothTime);

            //change the focus position based on the current look ahead
            focusPosition += Vector2.right * currentLookAheadX;



            float   yValue = 0;
            float   xValue = 0;
            Vector3 temp;

            //move the camera, and make sure the camera is in front of the level (on the z-axis)

            /*
             * if (focusPosition.y > transform.position.y)
             * {
             *  yValue = transform.position.y + 0.1f;
             * } else if (focusPosition.y < transform.position.y)
             * {
             *  yValue = transform.position.y - 0.1f;
             * } else
             * {
             *  yValue = transform.position.y;
             * }
             *
             * if (focusPosition.x > transform.position.x)
             * {
             *  xValue = transform.position.x + 0.1f;
             * }
             * else if (focusPosition.x < transform.position.x)
             * {
             *  xValue = transform.position.x - 0.1f;
             * }else
             * {
             *  xValue = transform.position.x;
             * }
             *
             * temp = new Vector3(xValue, yValue, -10);
             *
             */

            int i = 100;

            while (i > 0)
            {
                //Check that the focus position is not out of bounds and move it back inside bounds if it is.
                if (blockedRight && focusPosition.x > transform.position.x)
                {
                    focusPosition.x = transform.position.x;
                }
                if (blockedLeft && focusPosition.x < transform.position.x)
                {
                    focusPosition.x = transform.position.x;
                }
                if (blockedTop && focusPosition.y > transform.position.y)
                {
                    focusPosition.y = transform.position.y;
                }
                if (blockedBottom && focusPosition.y < transform.position.y)
                {
                    focusPosition.y = transform.position.y;
                }

                transform.position = Vector3.MoveTowards((Vector3)transform.position, ((Vector3)focusPosition + Vector3.forward * -10), 0.0625f);
                i--;
            } //end while
        }
        else  // if we do not have a player for the camera to target
        {
            //find the player
            target = GameObject.FindWithTag("Player").GetComponent <Controller2D>();

            focusArea = new FocusArea(target.collider.bounds, focusAreaSize);
        }//end if/else
    }
    //Use LateUpdate here rather than Update because we want to wait for all of the player movements to be calculated
    void LateUpdate()
    {
        focusArea.Update(target.controller.collider.bounds);

        Vector2 focusPosition = focusArea.center + Vector2.up * verticalOffset;



        //HORIZONTAL SMOOTHING METHOD 1 - This method of horizontal smoothing causes the camera to move ahead of the player as long as the player is giving some sort of input

        //If the velocity of the focusArea is not zero, smooth it to its next position
        if (focusArea.velocity.x != 0)
        {
            //Get the direction we should look ahead
            lookAheadDirX = Mathf.Sign(focusArea.velocity.x);

            //Reset lookAheadResetTimer (because we reset it after the focusArea has not moved for two seconds)
            lookAheadResetTimer = 0;

            //If the player is holding the same direction the focusArea velocity is going AND if the player's input is not zero
            //set the targetLookAheadX to the lookAheadDirection multiplied by the lookAheadDistance,
            //and make the camera start moving that way
            if (Mathf.Sign(target.directionalInput.x) == Mathf.Sign(focusArea.velocity.x) && target.directionalInput.x != 0)
            {
                lookAheadStopped = false;
                targetLookAheadX = lookAheadDirX * lookAheadDstX;
            }

            //Else, if either of the above conditions are NOT true, check if lookAheadStopped is false (as in, we've been moving)
            else
            {
                //If lookAheadStopped is false, set it to true and mess with the targetLookAheadX
                if (!lookAheadStopped)
                {
                    lookAheadStopped = true;
                    //This line stops the camera from immediately smoothing to its furthest extent when only slightly moving out of the focusArea
                    targetLookAheadX = currentLookAheadX + (lookAheadDirX * lookAheadDstX - currentLookAheadX) / 4f;
                }
            }
        }
        //Else, if the velocity IS zero, and the camera is NOT centered, wait two seconds and then set it to be centered
        else if (focusArea.velocity.x == 0 && targetLookAheadX != 0)
        {
            lookAheadResetTimer += Time.deltaTime;
            if (lookAheadResetTimer >= lookAheadResetTimerLength)
            {
                targetLookAheadX    = 0;
                lookAheadResetTimer = 0;
            }
        }

        //Set the variable for how far we should be looking ahead
        currentLookAheadX = Mathf.SmoothDamp(currentLookAheadX, targetLookAheadX, ref smoothLookVelocityX, lookSmoothTimeX);
        //Add the vector to the focusArea's position
        focusPosition += Vector2.right * currentLookAheadX;



        //VERTICAL SMOOTHING METHOD 1 - This method of vertical smoothing causes the player to fall faster than the camera can move, so we wont use it
        if (GameMaster.JUMP_MODE == 1)
        {
            lookSmoothTimeY = 0.1f;
            focusPosition.y = Mathf.SmoothDamp(transform.position.y, focusPosition.y, ref smoothLookVelocityY, lookSmoothTimeY);
        }
        else
        {
            //VERTICAL SMOOTHING METHOD 2 - This method of vertical smoothing causes the camera to move ahead of the player so they can kinda see what they're falling into

            //If we are moving down, set the targetLookAheadY to the velocity of the focusArea, scaled up by 4
            if (focusArea.velocity.y < 0)
            {
                if (GameMaster.JUMP_MODE == 2)
                {
                    lookSmoothTimeY = 0.4f;
                    if (focusArea.velocity.y > -0.22f)
                    {
                        //TODO: Make framerate independent.
                        //At 60fps, targetLookAhead reaches ~-2.1 units per frame.
                        //At 10fps, targetLookAhead reaches ~-1.2 units per frame.
                        //Look into ways later I could make this framerate independent
                        targetLookAheadY = focusArea.velocity.y * 10;                         //print (targetLookAheadY);
                    }
                    //TODO: Make framerate independent.
                }
                else if (GameMaster.JUMP_MODE == 3)
                {
                    lookSmoothTimeY  = 0.4f;
                    targetLookAheadY = -lookAheadDstY;
                }
            }
            //Else, if we are NOT moving down, center the vertical aspect of the camera on the player
            else
            {
                targetLookAheadY = 0;
            }

            //Set the variable for how far we should be looking ahead
            currentLookAheadY = Mathf.SmoothDamp(currentLookAheadY, targetLookAheadY, ref smoothLookVelocityY, lookSmoothTimeY);
            //Add the vector to the focusArea's position
            focusPosition += Vector2.up * currentLookAheadY;
        }



        //Finally apply all of the values we've calculated up to this point to the camera (also set the camera's Z position to something appropriate)
        transform.position = (Vector3)focusPosition + Vector3.forward * -10;
    }