예제 #1
0
        public static GameObject CreateDaggerfallEnemyGameObject(MobileTypes type, Transform parent, MobileReactions reaction)
        {
            DaggerfallUnity dfUnity = DaggerfallUnity.Instance;

            // Ensure enemy dict is loaded
            if (enemyDict == null)
            {
                enemyDict = EnemyBasics.GetEnemyDict();
            }

            GameObject go = new GameObject(string.Format("DaggerfallEnemy [{0}]", type.ToString()));

            if (parent)
            {
                go.transform.parent = parent;
            }
            go.transform.forward = Vector3.forward;

            // Add custom tag and script
            go.tag = dfUnity.Option_EnemyTag;
#if UNITY_EDITOR
            if (dfUnity.Option_CustomEnemyScript != null)
            {
                go.AddComponent(dfUnity.Option_CustomEnemyScript.GetClass());
            }
#endif

            // Add child object for enemy billboard
            GameObject mobileObject = new GameObject("DaggerfallMobileUnit");
            mobileObject.transform.parent = go.transform;

            // Add mobile enemy
            Vector2 size = Vector2.one;
            DaggerfallMobileUnit dfMobile = mobileObject.AddComponent <DaggerfallMobileUnit>();
            dfMobile.SetEnemy(dfUnity, enemyDict[(int)type], reaction);
            size = dfMobile.Summary.RecordSizes[0];

            // Add character controller
            if (dfUnity.Option_EnemyCharacterController || dfUnity.Option_EnemyExampleAI)
            {
                CharacterController controller = go.AddComponent <CharacterController>();
                controller.radius     = dfUnity.Option_EnemyRadius;
                controller.height     = size.y;
                controller.slopeLimit = dfUnity.Option_EnemySlopeLimit;
                controller.stepOffset = dfUnity.Option_EnemyStepOffset;

                // Reduce height of flying creatures as their wing animation makes them taller than desired
                // This helps them get through doors while aiming for player eye height
                if (dfMobile.Summary.Enemy.Behaviour == MobileBehaviour.Flying)
                {
                    controller.height /= 2f;
                }

                // Limit maximum height to ensure controller can fit through doors
                // For some reason Unity 4.5 doesn't let you set SkinWidth from code >.<
                if (controller.height > 1.9f)
                {
                    controller.height = 1.9f;
                }
            }

            // Add rigidbody
            if (dfUnity.Option_EnemyRigidbody)
            {
                Rigidbody rigidbody = go.AddComponent <Rigidbody>();
                rigidbody.useGravity  = dfUnity.Option_EnemyUseGravity;
                rigidbody.isKinematic = dfUnity.Option_EnemyIsKinematic;
            }

            // Add capsule collider
            if (dfUnity.Option_EnemyCapsuleCollider)
            {
                CapsuleCollider collider = go.AddComponent <CapsuleCollider>();
                collider.radius = dfUnity.Option_EnemyRadius;
                collider.height = size.y;
            }

            // Add navmesh agent
            if (dfUnity.Option_EnemyNavMeshAgent)
            {
                NavMeshAgent agent = go.AddComponent <NavMeshAgent>();
                agent.radius     = dfUnity.Option_EnemyRadius;
                agent.height     = size.y;
                agent.baseOffset = size.y * 0.5f;
            }

            // Add example AI
            if (dfUnity.Option_EnemyExampleAI)
            {
                // EnemyMotor will also add other required components
                go.AddComponent <Demo.EnemyMotor>();

                // Set sounds
                Demo.EnemySounds enemySounds = go.GetComponent <Demo.EnemySounds>();
                if (enemySounds)
                {
                    enemySounds.MoveSound   = (SoundClips)dfMobile.Summary.Enemy.MoveSound;
                    enemySounds.BarkSound   = (SoundClips)dfMobile.Summary.Enemy.BarkSound;
                    enemySounds.AttackSound = (SoundClips)dfMobile.Summary.Enemy.AttackSound;
                }
            }

            return(go);
        }
예제 #2
0
        public void setupMobile()
        {
            name = string.Format("DaggerfallEnemy [{0}]", creatureType.ToString());

            // Add child object for enemy billboard
            GameObject mobileObject = new GameObject("DaggerfallMobileUnit");

            mobileObject.transform.parent = this.transform;

            // Add mobile enemy
            Vector2 size = Vector2.one;
            DaggerfallMobileUnit dfMobile = mobileObject.AddComponent <DaggerfallMobileUnit>();

            try {
                Dictionary <int, MobileEnemy> enemyDict = EnemyBasics.GetEnemyDict();
                dfMobile.SetEnemy(dfUnity, enemyDict[(int)creatureType], reaction);
                size = dfMobile.Summary.RecordSizes[0];
            } catch (System.Exception e) {
                string message = string.Format("Failed to set enemy type (int)type={0}. '{1}'", (int)creatureType, e.Message);
                // TODO: Change logging
                DaggerfallUnity.LogMessage(message);
                GameObject.DestroyImmediate(dfMobile);
            }

            // Add character controller
            if (dfUnity.Option_EnemyCharacterController)
            {
                CharacterController controller = gameObject.AddComponent <CharacterController>();
                controller.radius     = dfUnity.Option_EnemyRadius;
                controller.height     = size.y;
                controller.slopeLimit = dfUnity.Option_EnemySlopeLimit;
                controller.stepOffset = dfUnity.Option_EnemyStepOffset;

                // Reduce height of flying creatures as their wing animation makes them taller than desired
                // This helps them get through doors while aiming for player eye height
                if (dfMobile.Summary.Enemy.Behaviour == MobileBehaviour.Flying)
                {
                    controller.height /= 2f;
                }

                // Limit maximum height to ensure controller can fit through doors
                // For some reason Unity 4.5 doesn't let you set SkinWidth from code >.<
                if (controller.height > 1.9f)
                {
                    controller.height = 1.9f;
                }
            }

            // Add rigidbody
            if (dfUnity.Option_EnemyRigidbody)
            {
                Rigidbody rigidbody = gameObject.AddComponent <Rigidbody>();
                rigidbody.useGravity  = dfUnity.Option_EnemyUseGravity;
                rigidbody.isKinematic = dfUnity.Option_EnemyIsKinematic;
            }

            // Add capsule collider
            if (dfUnity.Option_EnemyCapsuleCollider)
            {
                CapsuleCollider collider = gameObject.AddComponent <CapsuleCollider>();
                collider.radius = dfUnity.Option_EnemyRadius;
                collider.height = size.y;
            }

            // Add navmesh agent
            if (dfUnity.Option_EnemyNavMeshAgent)
            {
                NavMeshAgent agent = gameObject.AddComponent <NavMeshAgent>();
                agent.radius     = dfUnity.Option_EnemyRadius;
                agent.height     = size.y;
                agent.baseOffset = size.y * 0.5f;
            }

            // Add example AI
            if (dfUnity.Option_EnemyExampleAI)
            {
                // EnemyMotor will also add other required components
                gameObject.AddComponent <DaggerfallWorkshop.Demo.EnemyMotor>();

                // Set sounds
                DaggerfallWorkshop.Demo.EnemySounds enemySounds = gameObject.GetComponent <DaggerfallWorkshop.Demo.EnemySounds>();
                if (enemySounds)
                {
                    enemySounds.MoveSound   = (SoundClips)dfMobile.Summary.Enemy.MoveSound;
                    enemySounds.BarkSound   = (SoundClips)dfMobile.Summary.Enemy.BarkSound;
                    enemySounds.AttackSound = (SoundClips)dfMobile.Summary.Enemy.AttackSound;
                }
            }
        }