SoFunction
Updated on 2025-03-10

Unity implements the mouse to drag 3D objects

This article shares the specific code of unity to implement the mouse to drag 3D objects for your reference. The specific content is as follows

Just hang the script directly on the object to be dragged

using ;
using ;
using UnityEngine;

public class ModelDrages : MonoBehaviour 
{

//The camera that emits raysprivate Camera cam;
// Objects that collide with raysprivate GameObject go;
//The name of the object that the ray collidespublic static string btnName;
private Vector3 screenSpace;
private Vector3 offset;
private bool isDrage = false;

// Use this for initialization
void Start ()
{
 cam = ;
}

// Update is called once per frame
 void Update ()
{
 //Overall initial position Ray ray = ();
 //Rays emitted from the camera to click coordinates RaycastHit hitInfo;
 if (isDrage == false)
 {
  if(Physics .Raycast (ray,out hitInfo))
  {
   //Shadow the rays and can only be seen in Scene view   (, );
   go = ;
   print(btnName);
   screenSpace = ();
   offset =  - (new Vector3(, , ));
   //The name of the object   btnName = ;
   //The name of the component  }
  else
  {
   btnName = null;
  }
 }
 if((0))
 {
  Vector3 currentScreenSpace = new Vector3(, , );
  Vector3 currentPosition = (currentScreenSpace) + offset;
  if (btnName != null)
  {
    = currentPosition;
  }
  isDrage = true;
 }
 else
 {
  isDrage = false;
 }
 }

}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.