123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- /// <summary>
- /// 拼图控制
- /// </summary>
- public class MoveDoor : MonoBehaviour
- {
- //执行此选择
- bool isSelected;
- float distanceRay; //定位点击时计算
- GameObject piece; //分配点击时,将移动。
- Transform[] piecesMove;
- //[HideInInspector]
- //public float positionZ = -0.001f;
- [HideInInspector]
- public bool puzzlePlaying;
- Vector3 offset;
- GameObject lightC;
- public float positionX = 0f;
- public float positionY = 0f;
- public float positionZ = 0f;
- Vector3 positionL;
- //数字资源整合
- [Header("SFX")]
- public AudioSource clickSFX;
- public GameObject menu;
- public bool acitve = true;
- public bool b2A = false;
- public bool b3A = false;
- public bool b5A = false;
- private void Start()
- {
- }
- void Update()
- {
- //鼠标信息
- //按下鼠标
- if (Input.GetMouseButtonDown(0))
- {
- TouchClick();
- }
- //移动鼠标
- if (isSelected)
- {
- MovePiece();
- }
- //释放鼠标
- if (Input.GetMouseButtonUp(0))
- {
- FreePiece();
- }
- }
- /// <summary>
- /// 定位点击点
- /// </summary>
- void TouchClick()
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //从相机到点的触控点
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit))
- { //接收触控,如果有击中目标
- //print(hit.collider.transform.parent.tag);
- //print(hit.collider.transform.parent.name);
- if (hit.collider.gameObject.tag == "shui")
- {
- distanceRay = hit.distance;
- piece = hit.collider.gameObject;
- offset = hit.point - hit.collider.gameObject.transform.position;
- isSelected = true;
- }
- else if (
- hit.collider.gameObject.transform.parent &&
- (
- (hit.collider.gameObject.transform.parent.name == "group3" && hit.collider.transform.parent.tag == "shui") ||
- (hit.collider.gameObject.transform.parent.name == "group2" && hit.collider.transform.parent.tag == "shui") ||
- (hit.collider.gameObject.transform.parent.name == "group" && hit.collider.transform.parent.tag == "shui") ||
- (hit.collider.gameObject.transform.parent.name == "sgroup" && hit.collider.transform.parent.tag == "shui") ||
- (hit.collider.gameObject.transform.parent.name == "group4" && hit.collider.transform.parent.tag == "shui")
- )
- )
- {
- distanceRay = hit.distance;
- piece = hit.collider.gameObject.transform.parent.gameObject;
- offset = hit.point - hit.collider.gameObject.transform.parent.position;
- isSelected = true;
- }
- else
- {
- isSelected = false;
- }
- }
- }
- /// <summary>
- /// 移动碎片(每帧调用)
- /// </summary>
- void MovePiece()
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- Vector3 limitRay = ray.GetPoint(distanceRay);
- limitRay = new Vector3(limitRay.x, limitRay.y, limitRay.z);
- piece.transform.position = limitRay - offset;
- }
- /// <summary>
- /// 释放碎片
- /// </summary>
- void FreePiece()
- {
- if (isSelected)
- {
- GameObject b6 = GameObject.Find("group3/b6");
- float b6X = b6.transform.position.x;
- float b6Y = b6.transform.position.y;
- float b6Z = b6.transform.position.z;
- GameObject b7 = GameObject.Find("group2/b7");
- float b7X = b7.transform.position.x;
- float b7Y = b7.transform.position.y;
- float b7Z = b7.transform.position.z;
- GameObject door = GameObject.Find("group4/door");
- float doorX = door.transform.position.x;
- float doorY = door.transform.position.y;
- float doorZ = door.transform.position.z;
- GameObject ai = GameObject.Find("sgroup/ai");
- float aiX = ai.transform.position.x;
- float aiY = ai.transform.position.y;
- float aiZ = ai.transform.position.z;
- GameObject b4 = GameObject.Find("group/b4");
- float b4X = b4.transform.position.x;
- float b4Y = b4.transform.position.y;
- float b4Z = b4.transform.position.z;
- if (piece.name == "b2")
- {
- if (
- (b6X - piece.transform.position.x < 0.8f && b6X - piece.transform.position.x > -0.5f) &&
- (b6Y - piece.transform.position.y < 0 && b6Y - piece.transform.position.y > -3f) &&
- (b6Z - piece.transform.position.z < 0.5f && b6Z - piece.transform.position.z > -0.5f)
- )
- {
- piece.transform.SetParent(b6.transform.parent);
- piece.transform.position = new Vector3(b6X - 0.29f, b6Y + 2.3f, b6Z);
- clickSFX.Play();
- piece.tag = piece.name;
- b2A = true;
- }
- }
- if (piece.name == "b3")
- {
- if (
- (b6X - piece.transform.position.x < -2 && b6X - piece.transform.position.x > -4) &&
- (b6Y - piece.transform.position.y < 0 && b6Y - piece.transform.position.y > -3f) &&
- (b6Z - piece.transform.position.z < 0.5f && b6Z - piece.transform.position.z > -0.5f)
- )
- {
- piece.transform.SetParent(b6.transform.parent);
- piece.transform.position = new Vector3(b6X + 3.1f, b6Y + 2.3f, b6Z);
- clickSFX.Play();
- piece.tag = piece.name;
- b3A = true;
- }
- }
- if (piece.name == "b5")
- {
- if (
- (b6X - piece.transform.position.x < -0.7f && b6X - piece.transform.position.x > -2) &&
- (b6Y - piece.transform.position.y < -3 && b6Y - piece.transform.position.y > -5.1f) &&
- (b6Z - piece.transform.position.z < 0.5f && b6Z - piece.transform.position.z > -0.5f) && b2A && b3A
- )
- {
- piece.transform.SetParent(b6.transform.parent);
- piece.transform.position = new Vector3(b6X + 1.25f, b6Y + 4.65f, b6Z + 0.13f);
- clickSFX.Play();
- piece.tag = piece.name;
- b5A = true;
- }
- }
- if (piece.name == "duo2")
- {
- if (
- (b6X - piece.transform.position.x < -2.25f && b6X - piece.transform.position.x > -3.3f) &&
- (b6Y - piece.transform.position.y < -4 && b6Y - piece.transform.position.y > -5.4f) &&
- (b6Z - piece.transform.position.z < 0.5f && b6Z - piece.transform.position.z > -0.5f) && b2A && b3A && b5A
- )
- {
- piece.transform.SetParent(b6.transform.parent);
- piece.transform.position = new Vector3(b6X + 2.75f, b6Y + 4.9f, b6Z + 0.22f);
- clickSFX.Play();
- piece.tag = piece.name;
- }
- }
- if (piece.name == "duo1")
- {
- if (
- (b7X - piece.transform.position.x < 0.5f && b7X - piece.transform.position.x > -0.5f) &&
- (b7Y - piece.transform.position.y < 0.5f && b7Y - piece.transform.position.y > -0.5f) &&
- (b7Z - piece.transform.position.z < 0.5f && b7Z - piece.transform.position.z > -0.5f)
- )
- {
- piece.transform.SetParent(b7.transform.parent);
- piece.transform.position = new Vector3(b7X + 0.1f, b7Y + 0.14f, b7Z);
- clickSFX.Play();
- piece.tag = piece.name;
- }
- }
- if (piece.name == "group2")
- {
- if (
- (doorX - piece.transform.position.x < -0.5f && doorX - piece.transform.position.x > -1.6f) &&
- (doorY - piece.transform.position.y < -1.5f && doorY - piece.transform.position.y > -2.55f) &&
- (doorZ - piece.transform.position.z < 0.5f && doorZ - piece.transform.position.z > -0.5f) && b7.transform.parent.childCount == 2
- )
- {
- piece.transform.SetParent(door.transform.parent);
- piece.transform.position = new Vector3(doorX + 1.1f, doorY + 2.05f, doorZ);
- clickSFX.Play();
- piece.tag = "TempTag";
- }
- }
- if (piece.name == "group4")
- {
- print(b6X - piece.transform.position.x);
- print(b6Y - piece.transform.position.y);
- print(b6Z - piece.transform.position.z);
- if (
- (b6X - piece.transform.position.x < -0.7f && b6X - piece.transform.position.x > -2) &&
- (b6Y - piece.transform.position.y < -1.7f && b6Y - piece.transform.position.y > -3f) &&
- (b6Z - piece.transform.position.z < 0.5f && b6Z - piece.transform.position.z > -0.5f) && b2A && b3A && b5A
- )
- {
- piece.transform.SetParent(b6.transform.parent);
- piece.transform.position = new Vector3(b6X + 1.37f, b6Y + 2.28f, b6Z);
- clickSFX.Play();
- piece.tag = "TempTag";
- }
- }
- if (piece.name == "group")
- {
- if (
- (b6X - piece.transform.position.x < 2f && b6X - piece.transform.position.x > 1f) &&
- (b6Y - piece.transform.position.y < -1.2f && b6Y - piece.transform.position.y > -2.25f) &&
- (b6Z - piece.transform.position.z < 0.5f && b6Z - piece.transform.position.z > -0.5f) && b2A
- )
- {
- piece.transform.SetParent(b6.transform.parent);
- piece.transform.position = new Vector3(b6X - 1.5f, b6Y + 1.75f, b6Z);
- clickSFX.Play();
- piece.tag = "TempTag";
- b5A = true;
- }
- }
- if (piece.name == "tou")
- {
- if (
- (aiX - piece.transform.position.x < 0.5f && aiX - piece.transform.position.x > -0.5f) &&
- (aiY - piece.transform.position.y < -0.45f && aiY - piece.transform.position.y > -1.45f) &&
- (aiZ - piece.transform.position.z < 0.5f && aiZ - piece.transform.position.z > -0.5f)
- )
- {
- piece.transform.SetParent(ai.transform.parent);
- piece.transform.position = new Vector3(aiX, aiY + 0.95f, aiZ);
- clickSFX.Play();
- piece.tag = piece.name;
- }
- }
- if (piece.name == "screen")
- {
- if (
- (aiX - piece.transform.position.x < 0.5f && aiX - piece.transform.position.x > -0.5f) &&
- (aiY - piece.transform.position.y < 0.5f && aiY - piece.transform.position.y > -0.5f) &&
- (aiZ - piece.transform.position.z < 0.5f && aiZ - piece.transform.position.z > -0.5f)
- )
- {
- piece.transform.SetParent(ai.transform.parent);
- piece.transform.position = new Vector3(aiX, aiY, aiZ + 0.1f);
- clickSFX.Play();
- piece.tag = piece.name;
- }
- }
- if (piece.name == "tuozhan")
- {
- if (
- (aiX - piece.transform.position.x < 0.5f && aiX - piece.transform.position.x > -0.5f) &&
- (aiY - piece.transform.position.y < 0.5f && aiY - piece.transform.position.y > -0.5f) &&
- (aiZ - piece.transform.position.z < 0.5f && aiZ - piece.transform.position.z > -0.5f)
- )
- {
- piece.transform.SetParent(ai.transform.parent);
- piece.transform.position = new Vector3(aiX, aiY, aiZ - 0.1f);
- clickSFX.Play();
- piece.tag = piece.name;
- }
- }
- if (piece.name == "sgroup")
- {
- if (
- (b4X - piece.transform.position.x < 0.5f && b4X - piece.transform.position.x > -0.5f) &&
- (b4Y - piece.transform.position.y < 0.5f && b4Y - piece.transform.position.y > -1.3f) &&
- (b4Z - piece.transform.position.z < 0.5f && b4Z - piece.transform.position.z > -0.5f)
- )
- {
- piece.transform.SetParent(b4.transform.parent);
- piece.transform.position = new Vector3(b4X - 0.07f, b4Y + 0.7f, b4Z + 0.05f);
- clickSFX.Play();
- piece.tag = "TempTag";
- }
- }
- if (b6.transform.parent.childCount == 7 && acitve && b7.transform.parent.childCount == 2 && door.transform.parent.childCount == 2 && ai.transform.parent.childCount == 4 && b4.transform.parent.childCount == 2)
- {
- //Instantiate(menu, GameObject.FindGameObjectWithTag("UI").transform);
- acitve = false;
- }
- else
- {
- acitve = true;
- }
- isSelected = false;
- }
- }
- }
|