/// <summary> /// 生成宠物 /// </summary> private void CreatePokemon() { //判断类型 Transform prefab; if (MainManager.Instance.pokemonType) { prefab = presetPrefab; } else { prefab = randomPrefab; } //设置位置 Vector3 position = new Vector3( MainManager.Instance.pokemonPosition.x + 1, -1.5f, MainManager.Instance.pokemonPosition.z + 1); //生成宠物 Transform tf = Instantiate(prefab); tf.localScale = new Vector3(0.2f, 0.2f, 0.2f); tf.position = position; //宠物面向camrea方向 GameObject go = new GameObject(); go.transform.position = new Vector3(cam.transform.position.x, tf.position.y, cam.transform.position.z); tf.LookAt(go.transform); Destroy(go); //设置id和类型 PokemonController pokemon = tf.GetComponent <PokemonController>(); pokemon.pokemonID = MainManager.Instance.pokemonID; pokemon.pokemonType = MainManager.Instance.pokemonType; }
/// <summary> /// 初始化随机宠物 /// </summary> private void InitializeRandomPokemon() { if (MainManager.Instance.gameInitialization) { //之前初始化过则直接使用之前的数据 randomPlace.position = MainManager.Instance.startPosition; } else { //否则,将随机宠物位置中心设置在玩家处 randomPlace.position = player.position; MainManager.Instance.startPosition = randomPlace.position; MainManager.Instance.gameInitialization = true; } Vector2[] randomPositions = MainManager.Instance.randomPositions; //遍历并生成宠物 for (int i = 0; i < randomPositions.Length; i++) { PokemonController pokemon = Instantiate <PokemonController>(prefab, randomPlace); pokemon.pokemonID = i; pokemon.pokemonType = false; pokemon.transform.localPosition = new Vector3(randomPositions[i].x, 0f, randomPositions[i].y); } }
/// <summary> /// 抓捕宠物 /// </summary> /// <param name="ray">射线</param> private void CatchPokemon(Ray ray) { //设置层,只检查第9层 int layerMask = 1 << 9; //射线检测 if (Physics.Raycast(ray, out RaycastHit hitInfo, 100f, layerMask)) { //生成特效 Instantiate(effectPrefab, hitInfo.transform.position, Quaternion.identity); //获取宠物信息并更新字典 PokemonController pokemon = hitInfo.transform.GetComponent <PokemonController>(); MainManager.Instance.UpdatePokemon(pokemon.pokemonType, pokemon.pokemonID); //删除宠物 Destroy(pokemon.gameObject); Invoke("CatchEnd", 5); } }
/// <summary> /// 点击宠物 /// </summary> /// <param name="ray">射线</param> private void HitPokemon(Ray ray) { //指定Layer9 int layerMask = 1 << 9; //如果射线碰到物体 if (Physics.Raycast(ray, out RaycastHit hitInfo, 100f, layerMask)) { PokemonController pokemon = hitInfo.transform.GetComponent <PokemonController>(); if (pokemon.catching) { pokemon.transform.parent = player; MainManager.Instance.ClickedPokemon( pokemon.pokemonID, pokemon.pokemonType, pokemon.transform.localPosition); } } }