要达到吸附效果则需要每个拼图上挂载碰撞体
达到整张图片的替换效果需要选中所有拼图部件把材质球拖上去
、
制作材料球
脚本挂载到随便一个空物体上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PinTu : MonoBehaviour
{
// 记录有没有拼图被选中
private GameObject selectedObject;
// 拼图预制体
GameObject[] dragObj;
// 记录拼图应该放的点
GameObject[] dropObj;
// Start is called before the first frame updateq
void Start()
{
dragObj = GameObject.FindGameObjectsWithTag("drag");
dropObj = GameObject.FindGameObjectsWithTag("drop");
foreach (var item in dragObj)
{
item.transform.position = new Vector3(Random.Range(-0.2f, 0.3f), 0.815f, Random.Range(-0.1f,0.4f));
}
}
// Update is called once per frame
void Update()
{
// 如果按下了鼠标左键
if (Input.GetMouseButtonDown(0))
{
// 选中的物体为空
if (selectedObject == null)
{
// 存储的射线信息
RaycastHit hit = CastRay();
// 碰到的物体有碰撞器
if (hit.collider != null)
{
// 如果标签不是drag直接返回
if (!hit.collider.CompareTag("drag"))
{
return;
}
// 碰到的物体标签是--drag,为selectedObject赋值
selectedObject = hit.collider.gameObject;
// 设置鼠标光标不可见
Cursor.visible = false;
}
}
// 选中物体之后,再按一下鼠标左键---放下物体
else
{
// 记录鼠标点击的点
Vector3 position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.WorldToScreenPoint(selectedObject.transform.position).z);
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(position);
#region 吸附
//遍历每个放置点,找到和鼠标点击最近的点
Vector3 tmpdrop = Vector3.zero;
float minDistance = 10.0f;
foreach (var item in dropObj)
{
if (Vector3.Distance(item.transform.position, worldPosition) <= minDistance)
{
minDistance = Vector3.Distance(item.transform.position, worldPosition);
tmpdrop = item.transform.position;
}
}
// 如果最小距离小于---限定值,说明在拼图位置上,就赋值,否则就是鼠标位置
if (minDistance < 0.04f)
{
// 赋值
selectedObject.transform.position = tmpdrop + new Vector3(0, 0.003f, 0);
}
else
{
selectedObject.transform.position = worldPosition;
}
#endregion
//selectedObject.transform.position = worldPosition;
selectedObject = null;
Cursor.visible = true;
}
}
// 如果selectedObject不为空,说明点击到了物体,物体跟随鼠标移动
if (selectedObject != null)
{
// position存储的是鼠标的x,y坐标,自身的z坐标---屏幕坐标
Vector3 position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.WorldToScreenPoint(selectedObject.transform.position).z);
// 将屏幕坐标,转换为世界坐标
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(position);
// 为选中的物体赋值--这里的y是固定的0.25
selectedObject.transform.position = new Vector3(worldPosition.x, 0.811f, worldPosition.z);
// 按下鼠标的右键,旋转
if (Input.GetMouseButtonDown(1))
{
selectedObject.transform.rotation = Quaternion.Euler(new Vector3(
selectedObject.transform.rotation.eulerAngles.x,
selectedObject.transform.rotation.eulerAngles.y + 90f,
selectedObject.transform.rotation.eulerAngles.z));
}
}
}
// 返回射线碰撞信息
private RaycastHit CastRay()
{
// 射线最远的点
Vector3 screenMousePosFar = new Vector3(
Input.mousePosition.x,
Input.mousePosition.y,
Camera.main.farClipPlane);
// 射线最近的点
Vector3 screenMousePosNear = new Vector3(
Input.mousePosition.x,
Input.mousePosition.y,
Camera.main.nearClipPlane);
Vector3 worldMousePosFar = Camera.main.ScreenToWorldPoint(screenMousePosFar);
Vector3 worldMousePosNear = Camera.main.ScreenToWorldPoint(screenMousePosNear);
RaycastHit hit;
Physics.Raycast(worldMousePosNear, worldMousePosFar - worldMousePosNear, out hit);
return hit;
}
}
标签:__,unity3d,鼠标,拼图,GameObject,Vector3,position,selectedObject
From: https://blog.csdn.net/m0_74289471/article/details/141570415