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

How to drag object one axis direction with mouse

Discussion in 'Scripting' started by Dielser, Jun 22, 2022.

  1. Dielser

    Dielser

    Joined:
    Jun 22, 2022
    Posts:
    4
    I need to drag object with axis direction Like unity editor how to made this thank you.


    move axis.png
     
  2. Dielser

    Dielser

    Joined:
    Jun 22, 2022
    Posts:
    4
    Code (CSharp):
    1. public class DragAxis : MonoBehaviour
    2. {
    3.     // Start is called before the first frame update
    4.     Vector3 faceDirection;
    5.     GameObject target;
    6.     void Update()
    7.     {
    8.         FindObject();
    9.         DragObject();
    10.     }
    11.     void DragObject()
    12.     {
    13.         if(target != null)
    14.         {
    15.             Vector2 delta = Mouse.current.delta.ReadValue();
    16.             var angle = Mathf.Atan2 (delta.y, delta.x) * Mathf.Rad2Deg;
    17.             Vector3 direction = faceDirection * angle;
    18.             target.transform.position += direction.normalized* delta.magnitude*Time.deltaTime;
    19.         }
    20.     }
    21.     void FindObject()
    22.     {
    23.         if(Input.GetMouseButtonDown(0))
    24.         {
    25.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    26.             if(Physics.Raycast(ray,out RaycastHit hit))
    27.             {
    28.                 faceDirection = hit.normal;
    29.                 target = hit.collider.gameObject;
    30.             }
    31.         }
    32.         if(Input.GetMouseButtonUp(0))
    33.         {
    34.             target = null;
    35.         }
    36.     }
    37. }
    38.  
    Here my code still not working
     
  3. bplc

    bplc

    Joined:
    Mar 10, 2022
    Posts:
    104
    If I understand correctly you want to move an object, when you click with your mouse on a specific axis, like an elevator door, is that right ?
     
  4. Dielser

    Dielser

    Joined:
    Jun 22, 2022
    Posts:
    4
    something like that,I need to control object on one axis same as drag object with transform axis in unity editor

    dragObject.gif
     
  5. bplc

    bplc

    Joined:
    Mar 10, 2022
    Posts:
    104
    Very good, so only on one axis if I understood correctly.
     
  6. bplc

    bplc

    Joined:
    Mar 10, 2022
    Posts:
    104
    I have several ideas, but someone has already created a similar script, and distributes it for free, I leave it to you to study this script.
    you put the script on the gameObject of your choice on your scene, and you create the "drag" tag on your object to move, that's it.

    You will just have to change the axis in your code if necessary.

    Script :
    https://unicornone.gumroad.com/l/draganddrop3d

    Tuto youtube :



    Good luck ;)
     
    Last edited: Jun 23, 2022
  7. bplc

    bplc

    Joined:
    Mar 10, 2022
    Posts:
    104
    I think it can also help you to understand :)
    https://forum.unity.com/threads/sol...d-drop-using-x-and-y-from-screenspace.488476/



    EDIT :
    No answer, I hope that suits you ?

    In the link above in the Unity forum LiterallyJeff posted some code in his first answer which is quite simple to understand, you can embed it simply by copying it into a new script in the Updates tags, then you create a condition with a GetMouseButtonDown input, a bool and you have your drag and drop axis in simplified mode;)

    EDIT 2 :
    I don't know what you intend to do with these scripts, but don't forget that you can freeze the position/rotation of a rigidbody if you need.

    If you need more explanation don't hesitate to ask, but I think that with these two scripts you have plenty to do.
     
    Last edited: Jun 23, 2022
  8. Dielser

    Dielser

    Joined:
    Jun 22, 2022
    Posts:
    4
    Thank you bplc. I think still not the way I want.
    i will try to code but wrong way to drag. drag_worngway.gif