Unity 射线移动物体Ray |
您所在的位置:网站首页 › unity鼠标拖动物体移动 › 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 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().enabled = false; ray2 = cameraa.ScreenPointToRay(Input.mousePosition); Debug.DrawLine(ray2.origin,hit2.point ); LayerMask lm = 1 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |