Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Drag 3D Object with mouse on its local position

Discussion in 'Scripting' started by ki_ha1984, Dec 11, 2014.

  1. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    Hi,

    I need to drag objects during the game with Charp and mouse . Like you can see in the image below the objects are rotated in different position.



    With the code below when i drag to Y both of them goes to new Y ok. When i drag the object B to his X it goes well to the new X, it goes exactly like the black arrows, But when i drag the object A, does not work good it drags again the object like the black arrow . I need to drag the object every time on his own axis like the axis show with the 3 colors (red, green, blue), especially i would prefer to drag the object on his x,y and to lock z axis.


    Any idea ?
    Any code sample ?


    Code (CSharp):
    1. private bool _mouseState = false;
    2.  
    3. void Update ()
    4.     {      
    5.         if (Input.GetMouseButtonDown (0)) {
    6.                     RaycastHit hitInfo;
    7.                     target = GetClickedObject (out hitInfo);
    8.                     _mouseState = true;
    9.                     }
    10.         if (Input.GetMouseButtonUp (0)) {
    11.             _mouseState = false;
    12.         }          
    13.                    
    14.         if (_mouseState) {
    15.             if (move) {
    16.                     float xx = Input.GetAxis("Mouse X")/10;
    17.                     float yy = Input.GetAxis("Mouse Y")/10;
    18.                     target.transform.position = new Vector3(target.transform.position.x + xx, target.transform.position.y+yy,target.transform.position.z);
    19.                     }
    20.         }
    21.     }
    Thank you in advance

    ki_ha1984
     
  2. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    Any idea ?

    Thanks
     
  3. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    more simple, i would say that i need somehow to set the Z axis of the object of the face that i clicked.

    Thanks
     
  4. Megolas

    Megolas

    Joined:
    Apr 6, 2013
    Posts:
    25
    Try using
    transform.localPosition = transform.localPosition + new Vector(xx, yy, 0);
     
  5. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    Thank you Megolas,

    It makes the same problem with my code.

    Ki_ha1984
     
    Last edited: Dec 15, 2014
  6. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    Anyone with the same problem ?


    ki_ha1984
     
  7. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Updated your code. Try that :)
     
  8. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    Hi aaro4130.
    Thank you for post, but gives many errors, can you help with those ?
    All the error is from line 18.


    error CS1061: Type `UnityEngine.Transform' does not contain a definition for `left' and no extension method `left' of type `UnityEngine.Transform' could be found (are you missing a using directive or an assembly reference?)
    error CS0019: Operator `+' cannot be applied to operands of type `float' and `UnityEngine.Vector3'
    error CS1502: The best overloaded method match for `UnityEngine.Vector3.Vector3(float, float, float)' has some invalid arguments
    error CS1503: Argument `#1' cannot convert `object' expression to type `floa
     
  9. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    Any help ?
     
  10. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,887
    Replace line 18 with this:

    Code (CSharp):
    1. target.transform.localPosition += target.transform.right * xx;
    2. target.transform.localPosition += target.transform.up * yy;
    The problem is you were not changing the Z position. It needs to change X and Z to move it relative/local sideways, otherwise it will always move along World X.

    To move it always to the right of the camera, you can change "target.transform.right" to "camerasTransform.right", etc.
     
  11. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    Dear Stardog,

    Thank you very much it works very well.
    I notice that in order to move the object local need to change also the z, but i didnt know how to make it.
    Now is perfect.

    Thank you again.