Search Unity

Mouse drag on X, Z axes?

Discussion in 'Scripting' started by ganon, Sep 23, 2009.

  1. ganon

    ganon

    Joined:
    Mar 20, 2009
    Posts:
    22
    Hi all, i'm newie on js scripting. I try to apply script in to game object to move on 2d X and Z axes.
    For to do that y follow a flasdevelop tutorial with this example code:

    Code (csharp):
    1.  
    2. function Update () {
    3. }
    4.  
    5. var screenSpace;
    6. var offset;
    7.  
    8. function OnMouseDown(){
    9.     //translate the cubes position from the world to Screen Point
    10.     screenSpace = Camera.main.WorldToScreenPoint(transform.position);
    11.      
    12.     //calculate any difference between the cubes world position and the mouses Screen position converted to a world point  
    13.     offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y, screenSpace.z));
    14.    
    15. }
    16.  
    17. /*
    18. OnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse.
    19. OnMouseDrag is called every frame while the mouse is down.
    20. */
    21.  
    22. function OnMouseDrag () {
    23.  
    24.     //keep track of the mouse position
    25.     var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);    
    26.  
    27.     //convert the screen mouse position to world point and adjust with offset
    28.     var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
    29.  
    30.     //update the position of the object in the world
    31.     transform.position = curPosition;
    32.      
    33. }
    34.  
    The code only move my object on X,Y axes :S.

    How can i modify this code to move with drag mouse on X and Z axes?

    Thanks
    Sorry my bad english.
     
  2. jmunozar

    jmunozar

    Joined:
    Jun 23, 2008
    Posts:
    1,091
    Hello!

    I think your problem resides here :p

    Code (csharp):
    1. var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);