如何将Unity中鼠标拖动3D物体功能改写为长尾关键词?

2026-04-18 23:182阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计546个文字,预计阅读时间需要3分钟。

如何将Unity中鼠标拖动3D物体功能改写为长尾关键词?

原文:本文实例为大家分享了Unity实现鼠标拖拽3D物体的具体代码,供大家参考,具体内容如下+把该脚本直接挂载在要拖拽的物体上+using System.Collections;using System.Collections.Generic;using UnityEngine;+C#+//鼠标拖拽3D物体+public class Drag3DObject : MonoBehaviour{+ //鼠标按下时记录物体的位置+ private Vector3 offset;+ //是否开始拖拽+ private bool isDragging=false;+ //开始拖拽+ public void OnMouseDown(){+ isDragging=true;+ offset=transform.position - Input.mousePosition;+ }+ //结束拖拽+ public void OnMouseUp(){+ isDragging=false;+ }+ //更新位置+ void Update(){+ if(isDragging){+ Vector3 newPosition=Input.mousePosition + offset;+ transform.position=newPosition;+ }+ }+}++

以下是一个Unity中实现3D物体鼠标拖拽功能的代码示例,适用于参考使用。代码直接挂在需要拖拽的物体上即可。

C#using System.Collections;using System.Collections.Generic;using UnityEngine;

// 鼠标拖拽3D物体public class Drag3DObject : MonoBehaviour{ // 鼠标按下时记录物体的位置 private Vector3 offset; // 是否开始拖拽 private bool isDragging=false;

如何将Unity中鼠标拖动3D物体功能改写为长尾关键词?

// 开始拖拽 public void OnMouseDown() { isDragging=true; offset=transform.position - Input.mousePosition; }

// 结束拖拽 public void OnMouseUp() { isDragging=false; }

// 更新位置 void Update() { if (isDragging) { Vector3 newPosition=Input.mousePosition + offset; transform.position=newPosition; } }}

本文实例为大家分享了unity实现鼠标拖住3D物体的具体代码,供大家参考,具体内容如下

把该脚本直接挂在要拖拽的物体上即可

using System.Collections; using System.Collections.Generic; using UnityEngine; public class ModelDrages : MonoBehaviour { //发射射线的摄像机 private Camera cam; //射线碰撞的物体 private GameObject go; //射线碰撞物体的名字 public static string btnName; private Vector3 screenSpace; private Vector3 offset; private bool isDrage = false; // Use this for initialization void Start () { cam = Camera.main; } // Update is called once per frame void Update () { //整体初始位置 Ray ray = cam.ScreenPointToRay(Input.mousePosition); //从摄像机发出到点击坐标的射线 RaycastHit hitInfo; if (isDrage == false) { if(Physics .Raycast (ray,out hitInfo)) { //划出射线 只有在Scene视图中才能看到 Debug.DrawLine(ray.origin, hitInfo.point); go = hitInfo.collider.gameObject; print(btnName); screenSpace = cam.WorldToScreenPoint(go.transform.position); offset = go.transform.position - cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z)); //物体的名字 btnName = go.name; //组件的名字 } else { btnName = null; } } if(Input.GetMouseButton(0)) { Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z); Vector3 currentPosition = cam.ScreenToWorldPoint(currentScreenSpace) + offset; if (btnName != null) { go.transform.position = currentPosition; } isDrage = true; } else { isDrage = false; } } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计546个文字,预计阅读时间需要3分钟。

如何将Unity中鼠标拖动3D物体功能改写为长尾关键词?

原文:本文实例为大家分享了Unity实现鼠标拖拽3D物体的具体代码,供大家参考,具体内容如下+把该脚本直接挂载在要拖拽的物体上+using System.Collections;using System.Collections.Generic;using UnityEngine;+C#+//鼠标拖拽3D物体+public class Drag3DObject : MonoBehaviour{+ //鼠标按下时记录物体的位置+ private Vector3 offset;+ //是否开始拖拽+ private bool isDragging=false;+ //开始拖拽+ public void OnMouseDown(){+ isDragging=true;+ offset=transform.position - Input.mousePosition;+ }+ //结束拖拽+ public void OnMouseUp(){+ isDragging=false;+ }+ //更新位置+ void Update(){+ if(isDragging){+ Vector3 newPosition=Input.mousePosition + offset;+ transform.position=newPosition;+ }+ }+}++

以下是一个Unity中实现3D物体鼠标拖拽功能的代码示例,适用于参考使用。代码直接挂在需要拖拽的物体上即可。

C#using System.Collections;using System.Collections.Generic;using UnityEngine;

// 鼠标拖拽3D物体public class Drag3DObject : MonoBehaviour{ // 鼠标按下时记录物体的位置 private Vector3 offset; // 是否开始拖拽 private bool isDragging=false;

如何将Unity中鼠标拖动3D物体功能改写为长尾关键词?

// 开始拖拽 public void OnMouseDown() { isDragging=true; offset=transform.position - Input.mousePosition; }

// 结束拖拽 public void OnMouseUp() { isDragging=false; }

// 更新位置 void Update() { if (isDragging) { Vector3 newPosition=Input.mousePosition + offset; transform.position=newPosition; } }}

本文实例为大家分享了unity实现鼠标拖住3D物体的具体代码,供大家参考,具体内容如下

把该脚本直接挂在要拖拽的物体上即可

using System.Collections; using System.Collections.Generic; using UnityEngine; public class ModelDrages : MonoBehaviour { //发射射线的摄像机 private Camera cam; //射线碰撞的物体 private GameObject go; //射线碰撞物体的名字 public static string btnName; private Vector3 screenSpace; private Vector3 offset; private bool isDrage = false; // Use this for initialization void Start () { cam = Camera.main; } // Update is called once per frame void Update () { //整体初始位置 Ray ray = cam.ScreenPointToRay(Input.mousePosition); //从摄像机发出到点击坐标的射线 RaycastHit hitInfo; if (isDrage == false) { if(Physics .Raycast (ray,out hitInfo)) { //划出射线 只有在Scene视图中才能看到 Debug.DrawLine(ray.origin, hitInfo.point); go = hitInfo.collider.gameObject; print(btnName); screenSpace = cam.WorldToScreenPoint(go.transform.position); offset = go.transform.position - cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z)); //物体的名字 btnName = go.name; //组件的名字 } else { btnName = null; } } if(Input.GetMouseButton(0)) { Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z); Vector3 currentPosition = cam.ScreenToWorldPoint(currentScreenSpace) + offset; if (btnName != null) { go.transform.position = currentPosition; } isDrage = true; } else { isDrage = false; } } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。