private void Update() { #if UNITY_IPHONE || UNITY_ANDROID if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) { Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position); RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity, 1 << LayerMask.NameToLayer("AttackNpc"))) { DungeonCollectController dungeon_collect = hitInfo.transform.GetComponent <DungeonCollectController>(); if (null == dungeon_collect) { return; } UpdateCollect(); } } #else if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity, 1 << LayerMask.NameToLayer("AttackNpc"))) { DungeonCollectController dungeon_collect = hitInfo.transform.GetComponent <DungeonCollectController>(); if (null == dungeon_collect) { return; } UpdateCollect(); } } #endif }
/// <summary> /// 创建关卡Npc; /// </summary> /// <returns></returns> public void CreateStageNpc(StageData stage) { if (stage == null) { return; } NpcData npcData = DataManager.s_NpcDataManager.GetData(stage.NpcID); if (npcData == null) { return; } Npc npc = new Npc(stage.NpcID); Quaternion rotate = Quaternion.Euler(new Vector3(0, stage.Orient * Mathf.Rad2Deg, 0)); GameObject prefab = ResourcesManager.Instance.LoadUnitObject(npcData.Model, UNIT_TYPE.NPC); GameObject npcobj = GameObject.Instantiate(prefab) as GameObject; npcobj.transform.position = stage.Position; npcobj.transform.rotation = rotate; UnityEngine.AI.NavMeshAgent agent = npcobj.AddComponent <UnityEngine.AI.NavMeshAgent>(); agent.speed = 0; agent.acceleration = 0; agent.angularSpeed = 0; agent.avoidancePriority = (int)NavmeshPriority.NPC; agent.height = npcData.Height; agent.radius = npcData.Radius; agent.stoppingDistance = agent.radius; /*NavMeshObstacle obstacle = npcobj.AddComponent<NavMeshObstacle>(); * obstacle.height = npcData.Height; * obstacle.radius = npcData.Radius; * obstacle.carving = true;*/ CapsuleCollider collider = npcobj.AddComponent <CapsuleCollider>(); collider.height = npcData.Height; collider.radius = npcData.Radius; collider.center = Vector3.up * (Mathf.Max(collider.height / 2.0f, collider.radius) + 0.03f); npcobj.SetLayerRecursively(LayerMask.NameToLayer("AttackNpc")); if (stage.NpcType == NpcTypes.Battle) { NpcController controller = npcobj.AddComponent <NpcController>(); controller.m_Npc = npc; controller.m_CurStageData = stage; m_StageNpcControllerDic.Add(stage.ID, controller); } else if (stage.NpcType == NpcTypes.TreasureBox) { TreasureBoxController controller = npcobj.AddComponent <TreasureBoxController>(); controller.m_Npc = npc; controller.m_CurStageData = stage; m_StageNpcControllerDic.Add(stage.ID, controller); } else if (stage.NpcType == NpcTypes.Stone || stage.NpcType == NpcTypes.Crystal || stage.NpcType == NpcTypes.Wood) { DungeonCollectController controller = npcobj.AddComponent <DungeonCollectController>(); controller.m_Npc = npc; controller.m_CurStageData = stage; m_StageNpcControllerDic.Add(stage.ID, controller); } }