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