123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- /// <summary>
- /// 拼图控制
- /// </summary>
- public class MoveCar : MonoBehaviour
- {
- //执行此选择
- bool isSelected;
- float distanceRay; //定位点击时计算
- GameObject piece; //分配点击时,将移动。
- Transform[] piecesMove;
- //[HideInInspector]
- //public float positionZ = -0.001f;
- [HideInInspector]
- public bool puzzlePlaying;
- public bool wheel1 = false;
- public bool wheel2 = false;
- 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 == "f1group1" && hit.collider.transform.parent.tag == "shui") ||
- (hit.collider.gameObject.transform.parent.name == "f1group2" && hit.collider.transform.parent.tag == "shui") ||
- (hit.collider.gameObject.transform.parent.name == "f2group" && hit.collider.transform.parent.tag == "shui") ||
- (hit.collider.gameObject.transform.parent.name == "f3group" && hit.collider.transform.parent.tag == "shui") ||
- (hit.collider.gameObject.transform.parent.name == "aigroup" && hit.collider.transform.parent.tag == "shui") ||
- (hit.collider.gameObject.transform.parent.name == "lun" && 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 f2 = GameObject.Find("f2group/f2");
- float f2positionX = f2.transform.position.x;
- float f2positionY = f2.transform.position.y;
- float f2positionZ = f2.transform.position.z;
- GameObject f11 = GameObject.Find("f1group1/f1");
- float f11positionX = f11.transform.position.x;
- float f11positionY = f11.transform.position.y;
- float f11positionZ = f11.transform.position.z;
- GameObject f12 = GameObject.Find("f1group2/f1");
- float f12positionX = f12.transform.position.x;
- float f12positionY = f12.transform.position.y;
- float f12positionZ = f12.transform.position.z;
- if (piece.name == "f1group1")
- {
- if (
- (f2positionX - piece.transform.position.x < 0.5f && f2positionX - piece.transform.position.x > -0.5f) &&
- (f2positionY - piece.transform.position.y < -0.4f && f2positionY - piece.transform.position.y > -1.4f) &&
- (f2positionZ - piece.transform.position.z < -0.5f && f2positionZ - piece.transform.position.z > -1.5f)
- )
- {
- piece.transform.SetParent(f2.transform.parent);
- piece.transform.position = new Vector3(f2positionX, f2positionY + 0.9f, f2positionZ + 1f);
- clickSFX.Play();
- piece.tag = "TempTag";
- }
- }
- if (piece.name == "f1group2")
- {
- if (
- (f2positionX - piece.transform.position.x < 0.5f && f2positionX - piece.transform.position.x > -0.5f) &&
- (f2positionY - piece.transform.position.y < -0.4f && f2positionY - piece.transform.position.y > -1.4f) &&
- (f2positionZ - piece.transform.position.z < 1.5f && f2positionZ - piece.transform.position.z > 0.5f)
- )
- {
- piece.transform.SetParent(f2.transform.parent);
- piece.transform.position = new Vector3(f2positionX, f2positionY + 0.9f, f2positionZ - 1f);
- clickSFX.Play();
- piece.tag = "TempTag";
- }
- }
- if (piece.name == "duo1")
- {
- if (
- (f11positionX - piece.transform.position.x < 0.5f && f11positionX - piece.transform.position.x > -1.5f) &&
- (f11positionY - piece.transform.position.y < 1.3f && f11positionY - piece.transform.position.y > -0.7f) &&
- (f11positionZ - piece.transform.position.z < 1.3f && f11positionZ - piece.transform.position.z > -0.7f)
- )
- {
- piece.transform.SetParent(f11.transform.parent);
- piece.transform.position = new Vector3(f11positionX + 0.52f, f11positionY - 0.3f, f11positionZ - 0.3f);
- clickSFX.Play();
- piece.tag = piece.name;
- }
- }
- if (piece.name == "duo2")
- {
- if (
- (f12positionX - piece.transform.position.x < 0.5f && f12positionX - piece.transform.position.x > -1.5f) &&
- (f12positionY - piece.transform.position.y < 1.3f && f12positionY - piece.transform.position.y > -0.7f) &&
- (f12positionZ - piece.transform.position.z < 0.7f && f12positionZ - piece.transform.position.z > -1.3f)
- )
- {
- piece.transform.SetParent(f12.transform.parent);
- piece.transform.position = new Vector3(f12positionX + 0.52f, f12positionY - 0.3f, f12positionZ + 0.3f);
- clickSFX.Play();
- piece.tag = piece.name;
- }
- }
- if (piece.name == "wheel")
- {
- if (
- (f11positionX - piece.transform.position.x < -0.3f && f11positionX - piece.transform.position.x > -2.3f) &&
- (f11positionY - piece.transform.position.y < 1.4f && f11positionY - piece.transform.position.y > -0.6f) &&
- (f11positionZ - piece.transform.position.z < 0.9f && f11positionZ - piece.transform.position.z > -1.1f) && !wheel1
- )
- {
- piece.transform.SetParent(f11.transform.parent);
- wheel1 = true;
- piece.transform.position = new Vector3(f11positionX + 1.3f, f11positionY - 0.4f, f11positionZ + 0.1f);
- clickSFX.Play();
- piece.tag = piece.name;
- }
- }
- if (piece.name == "wheel")
- {
- if (
- (f12positionX - piece.transform.position.x < -0.3f && f12positionX - piece.transform.position.x > -2.3f) &&
- (f12positionY - piece.transform.position.y < 1.4f && f12positionY - piece.transform.position.y > -0.6f) &&
- (f12positionZ - piece.transform.position.z < 1.1f && f12positionZ - piece.transform.position.z > -0.9f) && !wheel2
- )
- {
- piece.transform.SetParent(f12.transform.parent);
- wheel2 = true;
- piece.transform.position = new Vector3(f12positionX + 1.3f, f12positionY - 0.4f, f12positionZ - 0.1f);
- clickSFX.Play();
- piece.tag = piece.name;
- }
- }
- isSelected = false;
- }
- }
- }
|