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

Freezing Y axis for drag and drop script

Discussion in 'Scripting' started by slowpuke666, Jan 13, 2020.

  1. slowpuke666

    slowpuke666

    Joined:
    May 4, 2018
    Posts:
    7
    Hello everyone, can anyone explain how to freeze the move for Y axis in this script?
    Code (CSharp):
    1. private GameObject target;
    2.     private bool isMouseDragging;
    3.     private Vector3 screenPosition;
    4.     private Vector3 offset;
    5.  
    6.     GameObject ReturnClickedObject(out RaycastHit hit)
    7.     {
    8.         GameObject targetObject = null;
    9.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    10.         if (Physics.Raycast(ray.origin, ray.direction * 10, out hit) && (hit.collider.tag == "Draggable"))
    11.         {
    12.             targetObject = hit.collider.gameObject;
    13.         }
    14.         return targetObject;
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         if (Input.GetMouseButtonDown(0))
    20.         {
    21.             RaycastHit hitInfo;
    22.             target = ReturnClickedObject(out hitInfo);
    23.             if (target != null)
    24.             {
    25.                 target.transform.position = new Vector3(target.transform.position.x, -6f, target.transform.position.z);
    26.                 isMouseDragging = true;
    27.                 Debug.Log("our target position :" + target.transform.position);
    28.                 //Here we Convert world position to screen position.
    29.                 screenPosition = Camera.main.WorldToScreenPoint(target.transform.position);
    30.                 offset = target.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z));
    31.             }
    32.         }
    33.  
    34.         if (Input.GetMouseButtonUp(0))
    35.         {
    36.             isMouseDragging = false;
    37.             target.transform.position = new Vector3(2.5f, -6f, 40f);
    38.         }
    39.  
    40.         if (isMouseDragging)
    41.         {
    42.             //tracking mouse position.
    43.             Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z);
    44.  
    45.  
    46.             //convert screen position to world position with offset changes.
    47.             Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offset;
    48.             Debug.Log("camera" + Camera.main.ScreenToWorldPoint(currentScreenSpace));
    49.             Debug.Log("offset" + offset);
    50.  
    51.             //It will update target gameobject's current postion.
    52.             target.transform.position = currentPosition;
    53.         }
    54.  
    55.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Before line 52 you can set the
    currentPosition.y
    field equal to whatever you like.

    If you need to choose what to set it to, perhaps you should copy the
    y
    position to a member variable at the moment you want to freeze it, such as when the mouse goes down.
     
    slowpuke666 likes this.
  3. slowpuke666

    slowpuke666

    Joined:
    May 4, 2018
    Posts:
    7
    Thanks sir
     
    Kurt-Dekker likes this.