123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- /// <summary>
- /// 拼图控制
- /// </summary>
- public class MoveComponet : 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);
- print(hit.collider.gameObject.tag);
- print(hit.collider.gameObject);
- 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)
- {
- //piece.transform.position = new Vector3(piece.transform.position.x, piece.transform.position.y, piece.transform.position.z);
- //print(screan);
- print(piece.transform.position);
- isSelected = false;
- }
- }
- }
|