Exemple #1
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;
        }
        private void CreateCharacter()
        {
            string path = EditorUtility.SaveFilePanel("Save Character", "Assets/Infinite Runner/Prefabs/Characters", "Character.prefab", "prefab");

            if (path.Length != 0 && Application.dataPath.Length < path.Length)
            {
                var character = GameObject.Instantiate(model) as GameObject;

                // Add a PlayerController, which will add the required components
                PlayerController playerController = character.AddComponent <PlayerController>();
                playerController.horizontalSpeed     = horizontalSpeed;
                playerController.jumpHeight          = jumpHeight;
                playerController.slideDuration       = slideDuration;
                playerController.attackType          = attackType;
                playerController.closeAttackDistance = attackCloseDistance;
                playerController.farAttackDistance   = attackFarDistance;
                playerController.autoTurn            = autoTurn;
                playerController.autoJump            = autoJump;
                playerController.forwardSpeeds       = forwardSpeedList;

                // PlayerAnimation will automatically be added
                PlayerAnimation playerAnimation = character.GetComponent <PlayerAnimation>();
                playerAnimation.useMecanim = useMecanim;
                if (useMecanim)
                {
                    playerAnimation.runAnimationName            = runStateName;
                    playerAnimation.runJumpAnimationName        = jumpStateName;
                    playerAnimation.runSlideAnimationName       = slideStateName;
                    playerAnimation.runRightStrafeAnimationName = rightStrafeStateName;
                    playerAnimation.runLeftStrafeAnimationName  = leftStrafeStateName;
                    playerAnimation.attackAnimationName         = attackStateName;
                    playerAnimation.backwardDeathAnimationName  = backwardDeathStateName;
                    playerAnimation.forwardDeathAnimationName   = forwardDeathStateName;
                    playerAnimation.idleAnimationName           = idleStateName;
                }
                else
                {
                    playerAnimation.runAnimationName            = runAnimationName;
                    playerAnimation.runJumpAnimationName        = runJumpAnimationName;
                    playerAnimation.runSlideAnimationName       = runSlideAnimationName;
                    playerAnimation.runRightStrafeAnimationName = runRightStrafeAnimationName;
                    playerAnimation.runLeftStrafeAnimationName  = runLeftStrafeAnimationName;
                    playerAnimation.attackAnimationName         = attackAnimationName;
                    playerAnimation.backwardDeathAnimationName  = backwardDeathAnimationName;
                    playerAnimation.forwardDeathAnimationName   = forwardDeathAnimationName;
                    playerAnimation.idleAnimationName           = idleAnimationName;
                }

                // The Rigidbody will automatically be added
                Rigidbody rigidbody = character.GetComponent <Rigidbody>();
                rigidbody.useGravity  = false;
                rigidbody.isKinematic = true;
                rigidbody.constraints = RigidbodyConstraints.FreezeAll;

                // The Capsule Collider will automatically be added
                CapsuleCollider capsuleCollider = character.GetComponent <CapsuleCollider>();
                capsuleCollider.height = 2;
                capsuleCollider.center = new Vector3(0, 1, 0);

                if (attackType == AttackType.Projectile)
                {
                    ProjectileManager projectileManager = character.AddComponent <ProjectileManager>();
                    projectileManager.projectilePrefab = projectileObject;
                }

                // Set the correct layers/tags/starting position
                character.layer = LayerMask.NameToLayer("Player");
                character.tag   = "Player";
                character.transform.position = new Vector3(0, -1.2f, 0);

                // Add the coin magnet trigger
                GameObject coinMagnet = new GameObject("Coin Magnet Trigger");
                coinMagnet.layer = LayerMask.NameToLayer("CoinMagnet");
                coinMagnet.AddComponent <CoinMagnetTrigger>();
                SphereCollider coinMagnetCollider = coinMagnet.AddComponent <SphereCollider>();
                coinMagnetCollider.isTrigger       = true;
                coinMagnet.transform.parent        = character.transform;
                playerController.coinMagnetTrigger = coinMagnet;

                // Add the particles
                GameObject particles = new GameObject("Particles");
                particles.transform.parent = character.transform;

                playerController.coinCollectionParticleSystem          = CreateParticleSystem("Coin Collection", particles.transform);
                playerController.secondaryCoinCollectionParticleSystem = CreateParticleSystem("Secondary Coin Collection", particles.transform);
                playerController.collisionParticleSystem       = CreateParticleSystem("Collision", particles.transform);
                playerController.groundCollisionParticleSystem = CreateParticleSystem("Ground Collision", particles.transform);
                playerController.powerUpParticleSystem         = new ParticleSystem[(int)PowerUpTypes.None];
                playerController.powerUpParticleSystem[(int)PowerUpTypes.DoubleCoin]    = CreateParticleSystem("Double coin", particles.transform);
                playerController.powerUpParticleSystem[(int)PowerUpTypes.CoinMagnet]    = CreateParticleSystem("Coin Magnet", particles.transform);
                playerController.powerUpParticleSystem[(int)PowerUpTypes.Invincibility] = CreateParticleSystem("Invincibility", particles.transform);
                playerController.powerUpParticleSystem[(int)PowerUpTypes.SpeedIncrease] = CreateParticleSystem("Speed Increase", particles.transform);

                // Copy the particles if there is a source
                if (particleSource != null)
                {
                    // And copy the particles
                    CopyParticleSystem(particleSource.coinCollectionParticleSystem, playerController.coinCollectionParticleSystem);
                    CopyParticleSystem(particleSource.secondaryCoinCollectionParticleSystem, playerController.secondaryCoinCollectionParticleSystem);
                    CopyParticleSystem(particleSource.collisionParticleSystem, playerController.collisionParticleSystem);
                    CopyParticleSystem(particleSource.groundCollisionParticleSystem, playerController.groundCollisionParticleSystem);
                    CopyParticleSystem(particleSource.powerUpParticleSystem[(int)PowerUpTypes.DoubleCoin], playerController.powerUpParticleSystem[(int)PowerUpTypes.DoubleCoin]);
                    CopyParticleSystem(particleSource.powerUpParticleSystem[(int)PowerUpTypes.CoinMagnet], playerController.powerUpParticleSystem[(int)PowerUpTypes.CoinMagnet]);
                    CopyParticleSystem(particleSource.powerUpParticleSystem[(int)PowerUpTypes.Invincibility], playerController.powerUpParticleSystem[(int)PowerUpTypes.Invincibility]);
                    CopyParticleSystem(particleSource.powerUpParticleSystem[(int)PowerUpTypes.SpeedIncrease], playerController.powerUpParticleSystem[(int)PowerUpTypes.SpeedIncrease]);
                }

                // All done, create the prefab
                path = string.Format("Assets/{0}", path.Substring(Application.dataPath.Length + 1));
                AssetDatabase.DeleteAsset(path);
                var prefab = PrefabUtility.CreatePrefab(path, character);
                DestroyImmediate(character, true);
                AssetDatabase.ImportAsset(path);
                Selection.activeObject = prefab;
            }
        }
        public virtual void Start()
        {
            gameManager = GameManager.instance;
            playerAnimation = PlayerController.instance.GetComponent<PlayerAnimation>();
            startLayer = gameObject.layer;

            if (destructionAnimation && isDestructible) {
                destructionAnimation[destructionAnimationName].wrapMode = WrapMode.Once;
                destructionDelay = new WaitForSeconds(0.2f);
            }

            collideWithPlayer = true;
        }
        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;
        }