123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- /// <summary>
- /// 拼图控制
- /// </summary>
- public class MoveNecklace : 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;
- 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 == "GrupoPiezas" || (hit.collider.gameObject.transform.parent.name == "group" && 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)
- {
- lightC = GameObject.Find("light");
- GameObject screan = GameObject.Find("group/screan");
- positionL = lightC.transform.position;
- positionX = lightC.transform.position.x;
- positionY = lightC.transform.position.y;
- positionZ = lightC.transform.position.z;
- float screanpositionX = screan.transform.position.x;
- float screanpositionY = screan.transform.position.y;
- float screanpositionZ = screan.transform.position.z;
- //piece.transform.position = new Vector3(piece.transform.position.x, piece.transform.position.y, piece.transform.position.z);
- //print(screan);
- //print(screan.tag);
- if (piece.name == "group")
- {
- if (
- (positionX - piece.transform.position.x < 0.5f && positionX - piece.transform.position.x > -0.5f) &&
- (positionY - piece.transform.position.y < 0.5f && positionY - piece.transform.position.y > -0.5f) &&
- (positionZ - piece.transform.position.z < 0.35f && positionZ - piece.transform.position.z > -0.35f)
- )
- {
- piece.transform.SetParent(lightC.transform.parent);
- piece.transform.position = new Vector3(positionX, positionY - 0.1f, positionZ + 0.08f);
- clickSFX.Play();
- piece.tag = "screan";
- }
- }
- if (piece.name == "ai")
- {
- if (
- (positionX - piece.transform.position.x < 0.5f && positionX - piece.transform.position.x > -0.5f) &&
- (positionY - piece.transform.position.y < 0.5f && positionY - piece.transform.position.y > -0.5f) &&
- (positionZ - piece.transform.position.z < 0.35f && positionZ - piece.transform.position.z > -0.65f)
- )
- {
- piece.transform.SetParent(lightC.transform.parent);
- piece.transform.position = new Vector3(positionX, positionY - 0.1f, positionZ - 0.08f);
- clickSFX.Play();
- piece.tag = piece.name;
- }
- }
- if (piece.name == "tou")
- {
- if (
- (screanpositionX - piece.transform.position.x < 0.5f && screanpositionX - piece.transform.position.x > -0.5f) &&
- (screanpositionY - piece.transform.position.y < 1.5f && screanpositionY - piece.transform.position.y > -1.5f) &&
- (screanpositionZ - piece.transform.position.z < 0.5f && screanpositionZ - piece.transform.position.z > -0.5f)
- )
- {
- piece.transform.SetParent(screan.transform.parent);
- piece.transform.position = new Vector3(screanpositionX, screanpositionY + 0.7f, screanpositionZ);
- clickSFX.Play();
- piece.tag = piece.name;
- }
- }
- if (piece.name == "e1" && lightC.transform.parent.childCount == 3 && screan.transform.parent.childCount == 2)
- {
- if (
- (positionX - piece.transform.position.x < 1.3f && positionX - piece.transform.position.x > -0.4f) &&
- (positionY - piece.transform.position.y < 1.3f && positionY - piece.transform.position.y > 0.3f) &&
- (positionZ - piece.transform.position.z < 1.5f && positionZ - piece.transform.position.z > -1.5f)
- )
- {
- piece.transform.SetParent(lightC.transform.parent);
- piece.transform.position = new Vector3(positionX-0.55f, positionY - 0.8f, positionZ);
- clickSFX.Play();
- piece.tag = piece.name;
- }
- }
- if (piece.name == "e2" && lightC.transform.parent.childCount == 4 && screan.transform.parent.childCount == 2)
- {
- if (
- (positionX - piece.transform.position.x < 2.3f && positionX - piece.transform.position.x > 1.5f) &&
- (positionY - piece.transform.position.y < 1.5f && positionY - piece.transform.position.y > 0.5f) &&
- (positionZ - piece.transform.position.z < 0.5f && positionZ - piece.transform.position.z > -0.5f)
- )
- {
- piece.transform.SetParent(lightC.transform.parent);
- piece.transform.position = new Vector3(positionX-1.92f, positionY - 1f, positionZ);
- clickSFX.Play();
- piece.tag = piece.name;
- }
- }
- //print(lightC.transform.parent.childCount);
- if (lightC.transform.parent.childCount == 5 && acitve && screan.transform.parent.childCount == 2)
- {
- //Instantiate(menu, GameObject.FindGameObjectWithTag("UI").transform);
- acitve = false;
- }
- else
- {
- acitve = true;
- }
- isSelected = false;
- }
- }
- }
|