- Home /
Drag And drop objects mouse
im trying to make a script where if you look at a object you can press a button e.g M you can move that object anywhere you want
please help
thanx in advanced
I don't think your question has enough detail about what you want exactly for anyone to really help you.
Answer by okokok · Aug 15, 2012 at 05:56 PM
 #pragma strict
 
 private  var screenPoint:Vector3 ;
 private  var offset:Vector3;
 function  OnMouseDown() { screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
 
 offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
 }
 
 function OnMouseDrag() 
 
 
 
 {  var curScreenPoint:Vector3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
 
 var curPosition:Vector3   = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
 transform.position = curPosition;
 }
You Can Use This OK !!!
Looking around I found your code really usefull! But I need to move an object on the x,z axis only, I've tried switching the axis on code but that's not working, how can I modify that code so the object only moves on x,z?
     using UnityEngine;
      
     
     
     public class DragTest2:$$anonymous$$onoBehaviour
     {
         private Vector3 screenPoint;
         private Vector3 offset;
     
     
         void On$$anonymous$$ouseDown()
         { 
             screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
             offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
         }
      
         void On$$anonymous$$ouseDrag() 
         {  
             Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
      
             Vector3 curPosition   = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
             transform.position = curPosition;
         }
     }
 
c# conversion.
Really appreciate that sweet snippet, great work, been head scratching for about a month on this one.
The behavior of the script is a little hard to constrain, but if you want to constrain to a particular axis (2d im guessing from the xz comment)
 void On$$anonymous$$ouseDrag() 
     {  
         Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
  
         Vector3 curPosition   = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
         transform.position = new Vector3(curPosition.x, transform.position.y, curPosition.z);
     }
(I wouldn't recommend it though)
Answer by Berenger · Jun 23, 2012 at 04:25 PM
Pick the object with Physics.Raycast then move it with Plane.Raycast, the plane being defined by the initial position of the object and the vector initial pos -> Camera.
Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do I add gravity to an object? 1 Answer
Unity3D Pressure Plate request. 3 Answers
Limiting Drag and Drop 1 Answer
JavaScript and Ajax second addition Tutorial 11 case problem 2 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                