Unity 射线移动物体Ray
效果
1.
补充一下 精准点击物体拖拽的效果 点击选中需要移动的物体,拖动物体,
物体是3D物体,摄像机是正交摄像机,
移动物体代码如下
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraRayObj : MonoBehaviour { public Vector3 offects; private Transform cube; void Update() { if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; //给拖拽的物体一个tag值 JQR 方便区分 //鼠标按下 记录鼠标点击物体的点到物体坐标位置的插值 if (Physics.Raycast(ray, out hit) && hit.collider.tag == "JQR") { cube = hit.collider.transform; offects = hit.collider.transform.position - hit.point; } } if (Input.GetMouseButton(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; //射线打到后面板子,相当于物体的位置, 板子设置一个层级 if (Physics.Raycast(ray, out hit, 1000, 1 << 8) && cube != null) { //第一种方式 //cube.position = v3-Vector3.forward*v3.z + hit.point-Vector3.forward*hit.point.z; //第二种方式 cube.position = new Vector3(hit.point.x + offects.x, hit.point.y + offects.y, cube.transform.position.z); } } if (Input.GetMouseButtonUp (0)) { cube = null; } } }
旋转物体的代码如下
using System.Collections; using System.Collections.Generic; using UnityEngine; public class RotatingObject : MonoBehaviour { //旋转最大角度 public int yMinLimit = -20; public int yMaxLimit = 80; //旋转速度 public float xSpeed = 250.0f; public float ySpeed = 120.0f; //旋转角度 private float x = 0.0f; private float y = 0.0f; private void Update() { if (Input.GetMouseButton (1)) { x -= Input.GetAxis("Mouse X") * xSpeed * 0.02f; y += Input.GetAxis("Mouse Y") * xSpeed * 0.02f; y = ClampAngle(y, yMinLimit, yMaxLimit); Quaternion rotation = Quaternion.Euler(y, x, 0); transform.rotation = rotation; } } //限制旋转角度 static float ClampAngle(float angle, float min, float max) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp(angle, min, max); } }
2.
实现射线拖动物体的功能,能上下左右的拖动,需要下载的小伙伴可以下载看看,
等下会把demo链接发上来,先把代码贴一下
写了俩个脚本一个是前后左右拖动,一个是上下拖动的,都是挂在同一个对象上,写的简单,不喜勿扰。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ShuiYiTuoDong : MonoBehaviour { Ray ray2; RaycastHit hit2; public Camera cameraa; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetMouseButton(0)) { this.GetComponent<ShengGaoCube>().enabled = false; ray2 = cameraa.ScreenPointToRay(Input.mousePosition); Debug.DrawLine(ray2.origin,hit2.point ); LayerMask lm = 1 << 9; if (Physics.Raycast(ray2, out hit2,lm)) { this.transform.position = new Vector3(hit2.point.x, transform.position.y, hit2.point.z); } } else { this.GetComponent<ShengGaoCube>().enabled = true; } } }
这是第二个
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ShengGaoCube : MonoBehaviour { Ray ray,ray1,ray2; RaycastHit hit,hit1,hit2; public Camera cameraa; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetKey (KeyCode .V)) { this.GetComponent<ShuiYiTuoDong>().enabled = false; if (Input.GetMouseButton(0)) { Debug.DrawLine(transform.position, hit.point); ray = cameraa.ScreenPointToRay(this.transform.position); LayerMask lm = 1 << 9; if (Physics.Raycast(ray, out hit, lm)) { } ray1 = cameraa.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray1, out hit1)) { if (hit1.transform.name == "Cube") { this.transform.position = new Vector3(this.transform.position.x, hit1.point.y, this.transform.position.z); } } } } else { this.GetComponent<ShuiYiTuoDong>().enabled = true; } } }
最后附上demo链接:
链接:https://pan.baidu.com/s/1DrGHn_j43rcb4hlj4OXnSw
提取码:mx1c
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器