Esempio n. 1
0
        // Move all of the objects within the LateObject to prevent jittering when the height transitions
        public void LateUpdate()
        {
            // don't move any objects if the game isn't active. The game may not be active if the character is in the air when they died
            if (!gameManager.IsGameActive())
            {
                return;
            }

            float forwardSpeed = forwardSpeeds.GetValue(totalMoveDistance);

            if (isStumbling)
            {
                forwardSpeed -= stumbleSpeedDecrement;
            }
            if (powerUpManager.IsPowerUpActive(PowerUpTypes.SpeedIncrease))
            {
                forwardSpeed += powerUpSpeedIncrease;
            }

            // continue along the curve if over a curved platform
            if (curveTime != -1 && platformObject != null)
            {
                curveTime = Mathf.Clamp01(curveMoveDistance / platformObject.curveLength);
                if (curveTime < 1 && followCurve)
                {
                    UpdateTargetPosition(thisTransform.eulerAngles.y);

                    // compute a future curve time to determine which direction the player will be heading
                    Vector3 curvePoint         = GetCurvePoint(curveMoveDistance, true);
                    float   futureMoveDistance = (curveMoveDistance + 2 * forwardSpeed * Time.deltaTime);
                    Vector3 futureCurvePoint   = GetCurvePoint(futureMoveDistance, false);
                    futureCurvePoint.y = curvePoint.y = targetPosition.y;
                    Vector3 forwardDir = (futureCurvePoint - curvePoint).normalized;
                    targetRotation = Quaternion.LookRotation(forwardDir);
                    infiniteObjectGenerator.SetMoveDirection(forwardDir);
                }
                curveMoveDistance += forwardSpeed * Time.deltaTime;
            }

            if (thisTransform.rotation != targetRotation)
            {
                thisTransform.rotation = Quaternion.RotateTowards(thisTransform.rotation, targetRotation,
                                                                  Mathf.Lerp(slowRotationSpeed, fastRotationSpeed, Mathf.Clamp01(Quaternion.Angle(thisTransform.rotation, targetRotation) / 45)));
            }

            playerAnimation.SetRunSpeed(forwardSpeed, forwardSpeedDelta != 0 ? (forwardSpeed - minForwardSpeed) / (forwardSpeedDelta) : 1);
            forwardSpeed      *= Time.deltaTime;
            totalMoveDistance += forwardSpeed;
            infiniteObjectGenerator.MoveObjects(forwardSpeed);
        }
Esempio n. 2
0
        public void Init()
        {
            // deprecated variables warnings:
            if (jumpForce != 0 && jumpHeight == 0)
            {
                Debug.LogError("PlayerController.jumpForce is deprecated. Use jumpHeight instead.");
                jumpHeight = jumpForce;
            }
            if (jumpDownwardForce != 0 && gravity == 0)
            {
                Debug.LogError("PlayerController.jumpDownwardForce is deprecated. Use gravity instead.");
                gravity = jumpDownwardForce;
            }
            // rigidbody should no longer use gravity, be kinematic, and freeze all constraints
            Rigidbody playerRigibody = GetComponent <Rigidbody>();

            if (playerRigibody != null)
            {
                if (playerRigibody.useGravity)
                {
                    Debug.LogError("The rigidbody no longer needs to use gravity. Disabling.");
                    playerRigibody.useGravity = false;
                }
                if (!playerRigibody.isKinematic)
                {
                    Debug.LogError("The rigidbody should be kinematic. Enabling.");
                    playerRigibody.isKinematic = true;
                }
                if (playerRigibody.constraints != RigidbodyConstraints.FreezeAll)
                {
                    Debug.LogError("The rigidbody should freeze all constraints. The PlayerController will take care of the physics.");
                    playerRigibody.constraints = RigidbodyConstraints.FreezeAll;
                }
            }

            cameraController        = CameraController.instance;
            infiniteObjectGenerator = InfiniteObjectGenerator.instance;
            powerUpManager          = PowerUpManager.instance;
            gameManager             = GameManager.instance;
            if (attackType == AttackType.Projectile)
            {
                projectileManager = GetComponent <ProjectileManager>();
            }

            platformLayer = 1 << LayerMask.NameToLayer("Platform");
            floorLayer    = 1 << LayerMask.NameToLayer("Floor");
            wallLayer     = 1 << LayerMask.NameToLayer("Wall");
            obstacleLayer = 1 << LayerMask.NameToLayer("Obstacle");

            thisTransform   = transform;
            capsuleCollider = GetComponent <CapsuleCollider>();
            playerAnimation = GetComponent <PlayerAnimation>();
            playerAnimation.Init();

            startPosition = thisTransform.position;
            startRotation = thisTransform.rotation;

            slideData   = new CoroutineData();
            stumbleData = new CoroutineData();
            forwardSpeeds.Init();
            // determine the fastest and the slowest forward speeds
            forwardSpeeds.GetMinMaxValue(out minForwardSpeed, out maxForwardSpeed);
            forwardSpeedDelta = maxForwardSpeed - minForwardSpeed;
            if (forwardSpeedDelta == 0)
            {
                playerAnimation.SetRunSpeed(1, 1);
            }

            ResetValues(false);
            enabled = false;
        }
        public void Init()
        {
            // deprecated variables warnings:
            if (jumpForce != 0 && jumpHeight == 0) {
                Debug.LogError("PlayerController.jumpForce is deprecated. Use jumpHeight instead.");
                jumpHeight = jumpForce;
            }
            if (jumpDownwardForce != 0 && gravity == 0) {
                Debug.LogError("PlayerController.jumpDownwardForce is deprecated. Use gravity instead.");
                gravity = jumpDownwardForce;
            }
            // rigidbody should no longer use gravity, be kinematic, and freeze all constraints
            Rigidbody playerRigibody = GetComponent<Rigidbody>();
            if (playerRigibody != null) {
                if (playerRigibody.useGravity) {
                    Debug.LogError("The rigidbody no longer needs to use gravity. Disabling.");
                    playerRigibody.useGravity = false;
                }
                if (!playerRigibody.isKinematic) {
                    Debug.LogError("The rigidbody should be kinematic. Enabling.");
                    playerRigibody.isKinematic = true;
                }
                if (playerRigibody.constraints != RigidbodyConstraints.FreezeAll) {
                    Debug.LogError("The rigidbody should freeze all constraints. The PlayerController will take care of the physics.");
                    playerRigibody.constraints = RigidbodyConstraints.FreezeAll;
                }
            }

            cameraController = CameraController.instance;
            infiniteObjectGenerator = InfiniteObjectGenerator.instance;
            powerUpManager = PowerUpManager.instance;
            gameManager = GameManager.instance;
            if (attackType == AttackType.Projectile) {
                projectileManager = GetComponent<ProjectileManager>();
            }

            platformLayer = 1 << LayerMask.NameToLayer("Platform");
            floorLayer = 1 << LayerMask.NameToLayer("Floor");
            wallLayer = 1 << LayerMask.NameToLayer("Wall");
            obstacleLayer = 1 << LayerMask.NameToLayer("Obstacle");

            thisTransform = transform;
            capsuleCollider = GetComponent<CapsuleCollider>();
            playerAnimation = GetComponent<PlayerAnimation>();
            playerAnimation.Init();

            startPosition = thisTransform.position;
            startRotation = thisTransform.rotation;

            slideData = new CoroutineData();
            stumbleData = new CoroutineData();
            forwardSpeeds.Init();
            // determine the fastest and the slowest forward speeds
            forwardSpeeds.GetMinMaxValue(out minForwardSpeed, out maxForwardSpeed);
            forwardSpeedDelta = maxForwardSpeed - minForwardSpeed;
            if (forwardSpeedDelta == 0) {
                playerAnimation.SetRunSpeed(1, 1);
            }

            ResetValues(false);
            enabled = false;
        }