Search Unity

Moving a object, towards the mouse position.

Discussion in 'Scripting' started by gilson, Apr 11, 2011.

  1. gilson

    gilson

    Joined:
    Sep 13, 2010
    Posts:
    36
    I have a script that you can drag objetcs while you hold the mouse button, but i was thinking to limit the speed of the object and then i would have to use translate. Anyone have any ideas, how can i translate a object to a mousePosition?

    I was using like this before:

    Code (csharp):
    1.  
    2. function Drag () {
    3.     if (selectedObject != null) {
    4.         selectedObject.position = MousePosition() + offset;
    5.     }
    6. }
    7.  
    8. function MousePosition () {
    9.     return Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, desiredZ) );
    10. }
    11.  


    But that aint gonna work with translate, since a have to pass a transform to be relative to and the mouse position is a vector3. Any ideas?

    Thank you.
     
  2. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,073
  3. gilson

    gilson

    Joined:
    Sep 13, 2010
    Posts:
    36
    Thanks man, i will give it a try!!
     
unityunity