123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- /// <summary>
- /// 拼图控制
- /// </summary>
- public class MoveCurtains : 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 c5r = false;
- public bool c5l = false;
- public bool duoA = false;
- public bool duo2A = false;
- public bool C3A = 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);
- 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 == "group" && 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 == "group3" && 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 C2 = GameObject.Find("group/C2");
- float C2X = C2.transform.position.x;
- float C2Y = C2.transform.position.y;
- float C2Z = C2.transform.position.z;
- GameObject ai = GameObject.Find("group2/ai");
- float aiX = ai.transform.position.x;
- float aiY = ai.transform.position.y;
- float aiZ = ai.transform.position.z;
- GameObject C1 = GameObject.Find("group3/C1");
- float C1X = C1.transform.position.x;
- float C1Y = C1.transform.position.y;
- float C1Z = C1.transform.position.z;
- GameObject C4 = GameObject.Find("group4/C4");
- float C4X = C4.transform.position.x;
- float C4Y = C4.transform.position.y;
- float C4Z = C4.transform.position.z;
- if (piece.name == "C5")
- {
- if (
- (C2X - piece.transform.position.x < 2.18f && C2X - piece.transform.position.x > 1.18f) &&
- (C2Y - piece.transform.position.y < -1.45f && C2Y - piece.transform.position.y > -2.45f) &&
- (C2Z - piece.transform.position.z < 0.5f && C2Z - piece.transform.position.z > -0.5f) && !c5r
- )
- {
- piece.transform.SetParent(C2.transform.parent);
- piece.transform.position = new Vector3(C2X - 1.68f, C2Y + 1.95f, C2Z);
- clickSFX.Play();
- c5r = true;
- piece.tag = piece.name;
- }
- else if (
- (C2X - piece.transform.position.x < -1.18f && C2X - piece.transform.position.x > -2.18f) &&
- (C2Y - piece.transform.position.y < -1.45f && C2Y - piece.transform.position.y > -2.45f) &&
- (C2Z - piece.transform.position.z < 0.5f && C2Z - piece.transform.position.z > -0.5f) && !c5l
- )
- {
- piece.transform.SetParent(C2.transform.parent);
- piece.transform.position = new Vector3(C2X + 1.68f, C2Y + 1.95f, C2Z);
- clickSFX.Play();
- c5l = true;
- piece.tag = piece.name;
- }
- }
- if (piece.name == "group3")
- {
- if (
- (C2X - piece.transform.position.x < -0.68f && C2X - piece.transform.position.x > -1.68f) &&
- (C2Y - piece.transform.position.y < -2.7f && C2Y - piece.transform.position.y > -3.7f) &&
- (C2Z - piece.transform.position.z < 0.5f && C2Z - piece.transform.position.z > -0.5f) && c5l && c5r
- )
- {
- piece.transform.SetParent(C2.transform.parent);
- piece.transform.position = new Vector3(C2X + 1.18f, C2Y + 3.2f, C2Z);
- clickSFX.Play();
- piece.tag = "TempTag";
- }
- }
- 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 == "group2")
- {
- if (
- (C1X - piece.transform.position.x < -1.15f && C1X - piece.transform.position.x > -2.15f) &&
- (C1Y - piece.transform.position.y < 0.6f && C1Y - piece.transform.position.y > -0.6f) &&
- (C1Z - piece.transform.position.z < 0.5f && C1Z - piece.transform.position.z > -0.5f)
- )
- {
- piece.transform.SetParent(C1.transform.parent);
- piece.transform.position = new Vector3(C1X + 1.65f, C1Y - 0.14f, C1Z + 0.06f);
- clickSFX.Play();
- piece.tag = "TempTag";
- }
- }
- if (piece.name == "duo1")
- {
- if (
- (C1X - piece.transform.position.x < -2.5f && C1X - piece.transform.position.x > -3.5f) &&
- (C1Y - piece.transform.position.y < 0.1f && C1Y - piece.transform.position.y > -0.9f) &&
- (C1Z - piece.transform.position.z < 0.5f && C1Z - piece.transform.position.z > -0.5f)
- )
- {
- piece.transform.SetParent(C1.transform.parent);
- piece.transform.position = new Vector3(C1X+3, C1Y+0.445f, C1Z);
- clickSFX.Play();
- piece.tag = piece.name;
- duoA = true;
- }
- }
- if (piece.name == "cur")
- {
- if (
- (C2X - piece.transform.position.x < 0.5f && C2X - piece.transform.position.x > -0.5f) &&
- (C2Y - piece.transform.position.y < -1.35f && C2Y - piece.transform.position.y > -2.35f) &&
- (C2Z - piece.transform.position.z < 0.3f && C2Z - piece.transform.position.z > -0.75f) && c5l && c5r
- )
- {
- piece.transform.SetParent(C2.transform.parent);
- piece.transform.position = new Vector3(C2X , C2Y + 1.85f, C2Z + 0.26f);
- clickSFX.Play();
- piece.tag = piece.name;
- }
- }
- if (piece.name == "duo2")
- {
- if (
- (C4X - piece.transform.position.x < -0.5f && C4X - piece.transform.position.x > -1.75f) &&
- (C4Y - piece.transform.position.y < 1.2f && C4Y - piece.transform.position.y > 0.2f) &&
- (C4Z - piece.transform.position.z < 0.5f && C4Z - piece.transform.position.z > -0.5f)
- )
- {
- piece.transform.SetParent(C4.transform.parent);
- piece.transform.position = new Vector3(C4X + 1.24f, C4Y - 0.7f, C4Z + 0.06f);
- clickSFX.Play();
- duo2A = true;
- piece.tag = piece.name;
- }
- }
- if (piece.name == "C3")
- {
- if (
- (C4X - piece.transform.position.x < 3.35f && C4X - piece.transform.position.x > 2.35f) &&
- (C4Y - piece.transform.position.y < 0.5f && C4Y - piece.transform.position.y > -0.5f) &&
- (C4Z - piece.transform.position.z < 0.5f && C4Z - piece.transform.position.z > -0.5f)
- )
- {
- piece.transform.SetParent(C4.transform.parent);
- piece.transform.position = new Vector3(C4X - 2.85f, C4Y+ 0.03f, C4Z);
- clickSFX.Play();
- C3A = true;
- piece.tag = piece.name;
- }
- }
- if (piece.name == "C6")
- {
- if (
- (C4X - piece.transform.position.x < 4.51f && C4X - piece.transform.position.x > 3.61f) &&
- (C4Y - piece.transform.position.y < 1.07f && C4Y - piece.transform.position.y > 0.17f) &&
- (C4Z - piece.transform.position.z < 0.5f && C4Z - piece.transform.position.z > -0.5f) && C3A
- )
- {
- piece.transform.SetParent(C4.transform.parent);
- piece.transform.position = new Vector3(C4X - 4.11f, C4Y - 0.67f, C4Z);
- clickSFX.Play();
- piece.tag = piece.name;
- }
- }
- if (piece.name == "group4")
- {
- if (
- (C1X - piece.transform.position.x < -1.1f && C1X - piece.transform.position.x > -2.1f) &&
- (C1Y - piece.transform.position.y < -0.9f && C1Y - piece.transform.position.y > -1.9f) &&
- (C1Z - piece.transform.position.z < 0.3f && C1Z - piece.transform.position.z > -0.7f) && duo2A && duoA
- )
- {
- piece.transform.SetParent(C1.transform.parent);
- piece.transform.position = new Vector3(C1X + 1.6f, C1Y + 1.4f, C1Z + 0.2f);
- clickSFX.Play();
- piece.tag = "TempTag";
- }
- }
- if (C2.transform.parent.childCount == 5 && acitve && ai.transform.parent.childCount == 4 && C1.transform.parent.childCount == 4 && C4.transform.parent.childCount == 4)
- {
- //Instantiate(menu, GameObject.FindGameObjectWithTag("UI").transform);
- acitve = false;
- }
- else
- {
- acitve = true;
- }
- isSelected = false;
- }
- }
- }
|