/// <summary> /// Sets up enemy based on current settings. /// </summary> public void ApplyEnemySettings() { DaggerfallUnity dfUnity = DaggerfallUnity.Instance; Dictionary <int, MobileEnemy> enemyDict = GameObjectHelper.EnemyDict; // Find mobile unit in children DaggerfallMobileUnit dfMobile = GetMobileBillboardChild(); if (dfMobile != null) { // Setup mobile billboard Vector2 size = Vector2.one; dfMobile.SetEnemy(dfUnity, enemyDict[(int)EnemyType], EnemyReaction); // Setup controller CharacterController controller = GetComponent <CharacterController>(); if (controller) { // Set base height from sprite size = dfMobile.Summary.RecordSizes[0]; controller.height = size.y; // 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; } // Uncomment below lines to limit maximum controller height // Some particularly tall sprites (e.g. giants) require this hack to get through doors // However they will appear sunken into ground as a result //if (controller.height > 1.9f) // controller.height = 1.9f; } // Setup sounds EnemySounds enemySounds = 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; } } }
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); }
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; } } }
/// <summary> /// Sets up enemy based on current settings. /// </summary> public void ApplyEnemySettings(MobileGender gender) { DaggerfallUnity dfUnity = DaggerfallUnity.Instance; Dictionary <int, MobileEnemy> enemyDict = GameObjectHelper.EnemyDict; MobileEnemy mobileEnemy = enemyDict[(int)EnemyType]; // Find mobile unit in children DaggerfallMobileUnit dfMobile = GetMobileBillboardChild(); if (dfMobile != null) { // Setup mobile billboard Vector2 size = Vector2.one; mobileEnemy.Gender = gender; dfMobile.SetEnemy(dfUnity, mobileEnemy, EnemyReaction, ClassicSpawnDistanceType); // Setup controller CharacterController controller = GetComponent <CharacterController>(); if (controller) { // Set base height from sprite size = dfMobile.Summary.RecordSizes[0]; controller.height = size.y; // 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 controller height // Some particularly tall sprites (e.g. giants) require this hack to get through doors if (controller.height > 1.78f) { // Adjust center so that sprite doesn't sink into the ground Vector3 newCenter = controller.center; newCenter.y += (1.78f - controller.height) / 2; controller.center = newCenter; controller.height = 1.78f; } controller.gameObject.layer = LayerMask.NameToLayer("Enemies"); } // Setup sounds EnemySounds enemySounds = GetComponent <Game.EnemySounds>(); if (enemySounds) { enemySounds.MoveSound = (SoundClips)dfMobile.Summary.Enemy.MoveSound; enemySounds.BarkSound = (SoundClips)dfMobile.Summary.Enemy.BarkSound; enemySounds.AttackSound = (SoundClips)dfMobile.Summary.Enemy.AttackSound; } // Setup entity if (entityBehaviour) { EnemyEntity entity = new EnemyEntity(entityBehaviour); entityBehaviour.Entity = entity; // Enemies are initially added to same world context as player entity.WorldContext = GameManager.Instance.PlayerEnterExit.WorldContext; int enemyIndex = (int)EnemyType; if (enemyIndex >= 0 && enemyIndex <= 42) { entityBehaviour.EntityType = EntityTypes.EnemyMonster; entity.SetEnemyCareer(mobileEnemy, entityBehaviour.EntityType); } else if (enemyIndex >= 128 && enemyIndex <= 146) { entityBehaviour.EntityType = EntityTypes.EnemyClass; entity.SetEnemyCareer(mobileEnemy, entityBehaviour.EntityType); } else { entityBehaviour.EntityType = EntityTypes.None; } } } }
/// <summary> /// Sets up enemy based on current settings. /// </summary> public void ApplyEnemySettings(MobileGender gender) { DaggerfallUnity dfUnity = DaggerfallUnity.Instance; Dictionary <int, MobileEnemy> enemyDict = GameObjectHelper.EnemyDict; MobileEnemy mobileEnemy = enemyDict[(int)EnemyType]; // Find mobile unit in children DaggerfallMobileUnit dfMobile = GetMobileBillboardChild(); if (dfMobile != null) { // Setup mobile billboard Vector2 size = Vector2.one; mobileEnemy.Gender = gender; dfMobile.SetEnemy(dfUnity, mobileEnemy, EnemyReaction); // Setup controller CharacterController controller = GetComponent <CharacterController>(); if (controller) { // Set base height from sprite size = dfMobile.Summary.RecordSizes[0]; controller.height = size.y; // 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; } // Uncomment below lines to limit maximum controller height // Some particularly tall sprites (e.g. giants) require this hack to get through doors // However they will appear sunken into ground as a result //if (controller.height > 1.9f) // controller.height = 1.9f; controller.gameObject.layer = LayerMask.NameToLayer("Enemies"); } // Setup sounds EnemySounds enemySounds = GetComponent <Game.EnemySounds>(); if (enemySounds) { enemySounds.MoveSound = (SoundClips)dfMobile.Summary.Enemy.MoveSound; enemySounds.BarkSound = (SoundClips)dfMobile.Summary.Enemy.BarkSound; enemySounds.AttackSound = (SoundClips)dfMobile.Summary.Enemy.AttackSound; } // Setup entity if (entityBehaviour) { EnemyEntity entity = new EnemyEntity(); entityBehaviour.Entity = entity; int enemyIndex = (int)EnemyType; if (enemyIndex >= 0 && enemyIndex <= 42) { entityBehaviour.EntityType = EntityTypes.EnemyMonster; entity.SetEnemyCareer(mobileEnemy, entityBehaviour.EntityType); } else if (enemyIndex >= 128 && enemyIndex <= 146) { entityBehaviour.EntityType = EntityTypes.EnemyClass; entity.SetEnemyCareer(mobileEnemy, entityBehaviour.EntityType); } else { entityBehaviour.EntityType = EntityTypes.None; } } } }
/// <summary> /// Sets up enemy based on current settings. /// </summary> public void ApplyEnemySettings(MobileGender gender) { DaggerfallUnity dfUnity = DaggerfallUnity.Instance; Dictionary <int, MobileEnemy> enemyDict = GameObjectHelper.EnemyDict; MobileEnemy mobileEnemy = enemyDict[(int)EnemyType]; if (AlliedToPlayer) { mobileEnemy.Team = MobileTeams.PlayerAlly; } // Find mobile unit in children DaggerfallMobileUnit dfMobile = GetMobileBillboardChild(); if (dfMobile != null) { // Setup mobile billboard Vector2 size = Vector2.one; mobileEnemy.Gender = gender; dfMobile.SetEnemy(dfUnity, mobileEnemy, EnemyReaction, ClassicSpawnDistanceType); // Setup controller CharacterController controller = GetComponent <CharacterController>(); if (controller) { // Set base height from sprite size = dfMobile.Summary.RecordSizes[0]; controller.height = size.y; // 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) { // (in frame 0 wings are in high position, assume body is the lower half) AdjustControllerHeight(controller, controller.height / 2, ControllerJustification.BOTTOM); } // Limit minimum controller height // Stops very short characters like rats from being walked upon if (controller.height < 1.6f) { AdjustControllerHeight(controller, 1.6f, ControllerJustification.BOTTOM); } controller.gameObject.layer = LayerMask.NameToLayer("Enemies"); } // Setup sounds EnemySounds enemySounds = GetComponent <Game.EnemySounds>(); if (enemySounds) { enemySounds.MoveSound = (SoundClips)dfMobile.Summary.Enemy.MoveSound; enemySounds.BarkSound = (SoundClips)dfMobile.Summary.Enemy.BarkSound; enemySounds.AttackSound = (SoundClips)dfMobile.Summary.Enemy.AttackSound; } MeshRenderer meshRenderer = dfMobile.GetComponent <MeshRenderer>(); if (meshRenderer) { if (dfMobile.Summary.Enemy.NoShadow) { meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; } if (dfMobile.Summary.Enemy.GlowColor != null) { meshRenderer.receiveShadows = false; GameObject enemyLightGameObject = Instantiate(LightAura); enemyLightGameObject.transform.parent = dfMobile.transform; enemyLightGameObject.transform.localPosition = new Vector3(0, 0.3f, 0.2f); Light enemyLight = enemyLightGameObject.GetComponent <Light>(); enemyLight.color = (Color)dfMobile.Summary.Enemy.GlowColor; enemyLight.shadows = DaggerfallUnity.Settings.DungeonLightShadows ? LightShadows.Soft : LightShadows.None; } } // Setup entity if (entityBehaviour) { EnemyEntity entity = new EnemyEntity(entityBehaviour); entityBehaviour.Entity = entity; // Enemies are initially added to same world context as player entity.WorldContext = GameManager.Instance.PlayerEnterExit.WorldContext; int enemyIndex = (int)EnemyType; if (enemyIndex >= 0 && enemyIndex <= 42) { entityBehaviour.EntityType = EntityTypes.EnemyMonster; entity.SetEnemyCareer(mobileEnemy, entityBehaviour.EntityType); } else if (enemyIndex >= 128 && enemyIndex <= 146) { entityBehaviour.EntityType = EntityTypes.EnemyClass; entity.SetEnemyCareer(mobileEnemy, entityBehaviour.EntityType); } else { entityBehaviour.EntityType = EntityTypes.None; } } } }